简体   繁体   English

如何将视图的 BackgroundColor 属性绑定到 Xamarin Forms 中的视图模型?

[英]How to bind the BackgroundColor property of a view to a View Model in Xamarin Forms?

So I am making an app in Xamarin Forms, using Fresh MVVM, and I want an event to be executed when a button is pressed, and only if the BackgroundColor of the button is white.所以我正在 Xamarin Forms 中制作一个应用程序,使用 Fresh MVVM,我希望在按下按钮时执行一个事件,并且仅当按钮的 BackgroundColor 为白色时。 The backgroundColor of the button will change in the XAML.CS, while this event will occur in the ViewModel.按钮的 backgroundColor 将在 XAML.CS 中更改,而此事件将在 ViewModel 中发生。

The problem is that with the current code that I have the property of my ViewModel has all primary colors and properties set to 0 instead of the actual button color properties.问题是,在当前代码中,我的 ViewModel 属性将所有原色和属性设置为 0,而不是实际的按钮颜色属性。 I have already looked for an answer, but nothing helped me.我已经在寻找答案,但没有任何帮助。

Here goes the XAML code:下面是 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>

ViewModel code:视图模型代码:

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
            });
        }
    }

And that is all, if you need more info to facilitate the solution I will provide it to you as soon as I see your request.这就是全部,如果您需要更多信息来促进解决方案,我会在看到您的请求后立即提供给您。 Thank you all for your time, hope you have a good day.谢谢大家的时间,希望你有一个美好的一天。

Major Binding modes are - ( Expect OneTime and Default(self-explanatory from name ))主要的绑定模式是 -( Expect OneTime 和 Default(名称不言自明))

  1. OneWay = the value change in ViewModel is set to the View(here button). OneWay = ViewModel 中的值更改设置为 View(此处为按钮)。 This is the default for most Properties.这是大多数属性的默认设置。

  2. TwoWay = The Value change in both ViewModel and View gets Notified. TwoWay = ViewModel 和 View 中的值更改都得到通知。 This is set to properties that change from the source side, like the Text property of Entry , SelectedItem property of ListView , and so on.这被设置为从源端更改的属性,例如EntryText属性、 ListView SelectedItem属性等。

  3. OneWayToSource = The value change in View is notified to ViewModel but value change in ViewModel is not notified to the View. OneWayToSource = View 中的值更改会通知 ViewModel,但 ViewModel 中的值更改不会通知 View。

The problem in your scenario is that the BindingMode of BackgroundColor property of Button is OneWay by default, it also does not make sense to keep BindingMode of BackgroundColor of Button as TwoWay as the value is not going to change from the control side.在方案中的问题是, BindingModeBackgroundColor的属性ButtonOneWay默认情况下,它也没有任何意义,以保持BindingModeBackgroundColorButtonTwoWay的价值不是从控制端去改变。

But the change in BackgroundColor property has to be reflected in ViewModel, Hence BindingMode has to be set to OneWayToSource .但在变化BackgroundColor属性在视图模型中得到体现,因此BindingMode必须被设置为OneWayToSource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM