Как заставить преобразованный в GridLength работать с GridUnitType.Star? - PullRequest
0 голосов
/ 07 июля 2019

Я получаю сообщение об ошибке при попытке преобразовать double в GridLength в звездных единицах. Сам преобразованный работает нормально, но я получаю ошибку во время выполнения

"System.ArgumentException: NaN не является допустимым значением для ширины \ n в Xamarin.Forms.Size..ctor (System.Double width, System.Double height) [0x00008] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Size.cs: 19 \ n в Xamarin.Forms.Grid.ContractColumnsIfNeeded (предикат System.Double width, System.Func`2 [T, TResult]) [0x00075] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 213 \ n в Xamarin.Forms.Grid.MeasureAndContractStarredColumns (System.Double width, System.Double height, System.Double totalStarsWidth) [0x000bb] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 397 \ n в Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x0010e] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 509 \ n в Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] в D: \ a \ 1 \ s \ Xamarin. Forms.Core \ GridCalc.cs: 58 \ n в Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ а \ \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 641 \ n в Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 523 \ n в Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 131 \ n в Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] в D: \ a \ 1 \ s \ Xamarin.Forms .Core \ VisualElement.cs: 581 \ n в Xamarin.Forms.Grid.MeasureStarredRows () [0x000c3] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 548 \ n в Xamarin.Forms .Grid.MeasureAndContractStarredRows (System.Double width, System.Double height, System.Double totalStarsHeight) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 403 \ n в Xamarin.Forms .Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x0011 7] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 510 \ n в Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 58 \ n в Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 641 \ n в Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] в D: \ a \ 1 \ s \ Xamarin.Forms .Core \ VisualElement.cs: 523 \ n в Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout. cs: 131 \ n в Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 581 \ n в Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.StackLayout + LayoutИнформационный макет, ориентация Xamarin.Forms.StackOrientation, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint) [0x000a8] в D: \ a \ 1 \ s \ Xamarin.Forms.Core\ StackLayout.cs: 163 \ n в Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.StackLayout + LayoutИнформационный макет, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint, System.Boolean processExpanders) [0x00058] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ StackLayout.cs: 123 \ n в Xamarin.Forms.StackLayout.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00019] вD: \ a \ 1 \ s \ Xamarin.Forms.Core \ StackLayout.cs: 80 \ n в Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 641 \ n в Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] в D: \ a \ 1 \ s\ Xamarin.Forms.Core \ VisualElement.cs: 523 \ n в Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 131 \ n в Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 581 \ n в Xamarin.Forms.Grid.CalculateAutoCells (System.Double width, System.Double height) [0x000e5] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: ​​131 \ n в Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x0000c] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: ​​483 \ n в Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ GridCalc.cs: 58 \n в Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 641 \ n в Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 523 \ n в Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \Layout.cs: 131 \ n в Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 581 \ n в Xamarin.Forms.ScrollView.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0005d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ ScrollView.cs: 233 \ n в Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 641 \ n вXamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 523 \ n в Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Двойное ограничение высоты) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 131 \ n в Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Флаги Forms.MeasureFlags) [0x00054] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 581 \ n в Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.StackLayout + Layoutininformation, Layout, информация, макет.Forms.StackOrientation ориентация, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint) [0x000a8] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ StackLayout.cs: 163\ n в Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.StackLayout + LayoutИнформационный макет, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint, System.Boolean pro cessExpanders) [0x00058] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ StackLayout.cs: 123 \ n в Xamarin.Forms.StackLayout.LayoutChildren (System.Double x, System.Double y, System.Двойная ширина, System.Double height) [0x0005b] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ StackLayout.cs: 57 \ n в Xamarin.Forms.Layout.UpdateChildrenLayout () [0x0014b] в D:\ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 263 \ n в Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] в D: \ a \ 1 \s \ Xamarin.Forms.Core \ Layout.cs: 223 \ n в Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 679 \ n в Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 908 \ n в Xamarin.Forms.VisualElement.set_Bounds (значение Xamarin.Forms.Rectangle) [0x0005d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 186 \ n в Xamarin.Forms.VisualElement.L ayout (Xamarin.Forms.Rectangle bounds) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 565 \ n в Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (дочерний элемент Xamarin.Forms.VisualElement, Xamarin.Область Forms.Rectangle) [0x001da] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 177 \ n в Xamarin.Forms.Page.LayoutChildren (System.Double x, System.Double y,System.Double width, System.Double height) [0x0010d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 187 \ n в Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] вD: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 259 \ n в Xamarin.Forms.Page.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] в D: \ a \1 \ s \ Xamarin.Forms.Core \ Page.cs: 240 \ n в Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 679 \ n в Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \VisualEleme nt.cs: 908 \ n на Xamarin.Forms.VisualElement.set_Bounds (значение Xamarin.Forms.Rectangle) [0x0005d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 186 \ n в Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle bounds) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 565 \ n в Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child, Xamarin.Forms.Область прямоугольника) [0x0005f] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 146 \ n в Xamarin.Forms.Page.LayoutChildren (System.Double x, System.Double y, System.Двойная ширина, System.Double height) [0x0010d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 187 \ n в Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] в D:\ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 259 \ n в Xamarin.Forms.Page.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] в D: \ a \ 1 \s \ Xamarin.Forms.Core \ Page.cs: 240 \ n в Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] в D: \ a \1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 679 \ n в Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 908 \ n в Xamarin.Forms.VisualElement.set_Bounds (значение Xamarin.Forms.Rectangle) [0x0005d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 186 \ n в Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle Bounds) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 565 \ n в Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (дочерний элемент Xamarin.Forms.VisualElement, регион Xamarin.Forms.Rectangle) [0x0005f] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 146 \ n в Xamarin.Forms.Page.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0010d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 187 \ nв Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 259 \ n в Xamarin.Forms.Page.OnSizeAllocated (System.Double width,Си ствол. Двойной онight) [0x0000f] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 240 \ n в Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000]в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 679 \ n в Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] в D: \ a\ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 908 \ n в Xamarin.Forms.VisualElement.set_Bounds (значение Xamarin.Forms.Rectangle) [0x0005d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 186 \ n в Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle bounds) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 565\ n в Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (дочерний элемент Xamarin.Forms.VisualElement, область Xamarin.Forms.Rectangle) [0x0005f] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Layout.cs: 146 \n в Xamarin.Forms.Page.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x00103] в D: \ a \ 1 \ s \ Xamarin.Forms.C ore \Page.cs: 185 \ n в Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 259 \ n в Xamarin.Forms.Page.OnSizeAllocated (System.Double width, System. Двойная высота) [0x0000f] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ Page.cs: 240 \ n в Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 679 \ n в Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] в D:\ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 908 \ n в Xamarin.Forms.VisualElement.set_Bounds (значение Xamarin.Forms.Rectangle) [0x0005d] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 186 \ n в Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle bounds) [0x00000] в D: \ a \ 1 \ s \ Xamarin.Forms.Core \ VisualElement.cs: 565 \ n в Xamarin.Forms.Platform.iOS.NavigationRenderer.ViewDidLayoutSubviews () [0x00172] в D: \ a \ 1 \ s \ Xamarin.Forms.Platform.iOS \ Renderers \ NavigationRe nderer.cs: 182 \ n в(Обертка, управляемая на родной) UIKit.UIApplication.UIApplicationMain (int, string [], intptr, intptr) \ n в UIKit.UIApplication.Main (аргументы System.String [], субъект System.IntPtr, делегат System.IntPtr) [0x00005] в / Library /Frameworks / Xamarin.iOS.framework / Versions / 12.4.0.64 / src / Xamarin.iOS / UIKit / UIApplication.cs: 79 \ n в UIKit.UIApplication.Main (System.String [], args, System.String PrincipalClassName, System.Строка DelegateClassName) [0x0002c] в /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs:63 \ n в PrenCare.iOS.Application.Main (система.Строка [] args) [0x00002] в C: \ Users \ ondre \ source \ repos \ PrenCare \ src \ Version02 \ PrenCare \ PrenCare.iOS \ Main.cs: 19 "

.Странно то, что конвертер отлично работает, когда я использую Абсолютный блок.

Конвертер

public class NumberToGridLengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is double numberValue))
        {
            throw new Exception($"Type {value.GetType().FullName} cannot be converted to GridLenght");
        }

        return new GridLength(numberValue, GridUnitType.Star); // Star makes it crash
        //return GridLength.Star; // Star makes it crash as well
        //return new GridLength(numberValue); // This works fine

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is GridLength gridLength))
        {
            throw new Exception($"Type {value.GetType().FullName} is not GridLenght");
        }

        return gridLength.Value;
    }
}

XAML

<ContentPage.Resources>
    <ResourceDictionary>
        <conv:NumberToGridLengthConverter x:Key="numberToGridLengthConverter" />
    </ResourceDictionary>  
</ContentPage.Resources>

<ContentPage.Content>
    <StackLayout>
        <Grid Grid.Column="1" ColumnSpacing="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{Binding Path=MovementCalm, Mode=TwoWay, Converter={StaticResource numberToGridLengthConverter}}" />
                <ColumnDefinition Width="{Binding Path=MovementRapid, Mode=TwoWay, Converter={StaticResource numberToGridLengthConverter}}" />
            </Grid.ColumnDefinitions>

            <BoxView Grid.Column="0" BackgroundColor="LightGreen" HorizontalOptions="FillAndExpand" HeightRequest="20" />
            <BoxView Grid.Column="1" BackgroundColor="Orange" HorizontalOptions="FillAndExpand" HeightRequest="20" />
        </Grid>
    </StackLayout>
</ContentPage.Content>

ViewModel

public class StatisticsViewModel : BaseViewModel
{
    private double _movementCalm;
    public double MovementCalm
    {
        get => _movementCalm;
        set => SetProperty(ref _movementCalm, value);
    }

    private double _movementRapid;
    public double MovementRapid
    {
        get => _movementRapid;
        set => SetProperty(ref _movementRapid, value);
    }

    public StatisticsViewModel()
    {
    }

    public override void OnAppearing()
    {
        base.OnAppearing();

        SetMovement();
    }

    private void SetMovement()
    {
        MovementCalm = 90f;
        MovementRapid = 10f;
    }
}


public abstract class BaseViewModel : INotifyPropertyChanged
{
    public virtual void OnAppearing()
    {
    }

    public virtual void OnBindingContextChanged()
    {
    }

    public virtual void OnDisappearing()
    {
    }

    #region INotifyPropertyChanged

    protected bool SetProperty<T>(
        ref T backingStore,
        T value,
        [CallerMemberName]string propertyName = "",
        Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
        {
            return false;
        }

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
        {
            return;
        }

        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

Есть идеи, как заставить его работать со звёздами Star?

1 Ответ

1 голос
/ 08 июля 2019

Во-первых, если вы установите GridUnitType на Star, ширина Column никогда не изменится (каждый Column будет занимать половину экрана, если у вас 2 Columns).

//
// Summary:
//     Interpret the Xamarin.Forms.GridLength.Value property value as a proportional
//     weight, to be laid out after rows and columns with Xamarin.Forms.GridUnitType.Absolute
//     or Xamarin.Forms.GridUnitType.Auto are accounted for.
//
// Remarks:
//     After all the rows and columns of type Xamarin.Forms.GridUnitType.Absolute and
//     Xamarin.Forms.GridUnitType.Auto are laid out, each of the corresponding remaining
//     rows or columns, which are of type Xamarin.Forms.GridUnitType.Star, receive a
//     fraction of the remaining available space. This fraction is determined by dividing
//     each row's or column's Xamarin.Forms.GridLength.Value property value by the sum
//     of all the row or column Xamarin.Forms.GridLength.Value property values, correspondingly,
//     including its own.
Star = 1,

Кроме того, когда вы устанавливаете Binding Path, вы должны одновременно установить BindingContext.

Например, я привязываю значение ширины кползунок.

<ContentPage.Content>
  <StackLayout>
     <Slider x:Name="slider" Maximum="200" Minimum="100" Value="100" />

     <Grid  Grid.Column="1" ColumnSpacing="0">
        <Grid.ColumnDefinitions>
             <ColumnDefinition BindingContext="{x:Reference slider}"  Width="{Binding Path=Value, Mode=TwoWay, Converter={StaticResource numberToGridLengthConverter}}" />
             <ColumnDefinition BindingContext="{x:Reference slider}"  Width="{Binding Path=Value, Mode=TwoWay, Converter={StaticResource numberToGridLengthConverter}}" />
        </Grid.ColumnDefinitions>

        <BoxView Grid.Column="0" BackgroundColor="LightGreen" HorizontalOptions="FillAndExpand" HeightRequest="20" />
        <BoxView Grid.Column="1" BackgroundColor="Orange" HorizontalOptions="FillAndExpand" HeightRequest="20" />
    </Grid>
  </StackLayout>
</ContentPage.Content>

Установка GridUnitType на Star не приведет к сбою.Поэтому, если ваша проблема все еще появляется, вы можете создать образец, содержащий проблему, чтобы я мог проверить ее на своей стороне.

Обновление:

У меня естьобнаружил вашу проблему!

Вы должны инициализировать MovementCalm и MovementRapid в конструкторе.

public StatisticsViewModel()
{
  SetMovement();
}

на вашей странице содержимого (такой как MainPage)

public MainPage()
{
  InitializeComponent();

  BindingContext = new StatisticsViewModel();
}

Метод OnAppearing и OnDisappearing никогда не будет вызван, поскольку ViewModel не является ContentPage .

Таким образом, значение ширины всегда 0 . При установке GridUnitType в качестве Star произойдет сбой.Но когда вы установите его как Абсолют , он не будет, потому что Абсолют допускает значение как 0 .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...