因此,我正在使用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>
ViewModel代码:
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
});
}
}
就是如此,如果您需要更多信息来简化解决方案,我将在收到您的请求后立即将其提供给您。谢谢大家的时间,希望您过得愉快。
主要的绑定模式是-(Expect OneTime和Default(从名称中不言自明))
OneWay
=将ViewModel中的值更改设置为View(此处按钮)。这是大多数属性的默认设置。
TwoWay
=在ViewModel和View中的值更改都得到通知。这被设置为属性,从源极侧变化,如Text
财产Entry
,SelectedItem
财产ListView
,等等。
OneWayToSource
= View中的值更改已通知给ViewModel,但ViewModel中的值更改未通知给View。
您的方案中的问题是,默认情况下of的BindingMode
ofBackgroundColor
属性Button
是OneWay
没有意义BindingMode
的BackgroundColor
,Button
因为TwoWay
值不会从控制端更改,因此保留of也是没有意义的。
但是BackgroundColor
属性的更改必须反映在ViewModel中,因此BindingMode必须设置为OneWayToSource
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句