1

因此,我正在使用 Fresh MVVM 在 Xamarin Forms 中制作应用程序,并且我希望在按下按钮时执行事件,并且仅当按钮的 BackgroundColor 为白色时才执行。按钮的 backgroundColor 将在 XAML.CS 中更改,而此事件将在 ViewModel 中发生。

问题是,在我拥有 ViewModel 属性的当前代码中,所有原色和属性都设置为 0,而不是实际的按钮颜色属性。我已经在寻找答案,但没有任何帮助。

下面是 XAML 代码:

<Button
            x:Name="Button_NextStep"
            HeightRequest="50"
            WidthRequest="400"
            BackgroundColor="{Binding NextStepBackgroundColor}"
            CornerRadius="30"

            Text="Next step"
            TextColor="#4847FF"
            FontAttributes="Bold"
            FontSize="20"

            VerticalOptions="Start"
            HorizontalOptions="Start"
            Margin="25,178,25,5"

            Command="{Binding NextStep}"
></Button>

视图模型代码:

class CreateAccount_UsernameViewModel: FreshBasePageModel
    {
        public ICommand NextStep { get; set; }
        public Color NextStepBackgroundColor { get; set; }

        public InavigationService navigationService; //this is irrelevant for this question

        public CreateAccount_UsernameViewModel(InavigationService _navService)
        {
            navigationService = _navService; //this is irrelevant for this question

            NextStep = new Command(() =>
            {
                if (NextStepBackgroundColor == Color.FromHex("#FFD3D3D3"))
                    navigationService.SwitchNavigationStacks(Enums.NavigationStacks.CreateAccount, this); //this is irrelevant for this question
            });
        }
    }

仅此而已,如果您需要更多信息来促进解决方案,我会在看到您的请求后立即提供给您。谢谢大家的时间,希望你有一个美好的一天。

4

1 回答 1

1

主要的绑定模式是 - (期望一次性和默认(从名称不言自明))

  1. OneWay= ViewModel 中的值更改设置为 View(此处按钮)。这是大多数属性的默认设置。

  2. TwoWay= ViewModel 和 View 中的值更改都会得到通知。这设置为从源端更改的属性,例如 的Text属性Entry、的SelectedItem属性ListView等。

  3. OneWayToSource= View 中的值更改会通知给 ViewModel,但 ViewModel 中的值更改不会通知给 View。

您的场景中的问题是ofBindingMode属性BackgroundColor默认情况下,保持of as也没有意义,因为值不会从控制端改变。ButtonOneWayBindingModeBackgroundColorButtonTwoWay

但是属性的变化BackgroundColor必须反映在 ViewModel 中,因此 BindingMode 必须设置为OneWayToSource.

于 2020-02-24T12:46:04.883 回答