gpt4 book ai didi

c# - 将 VisualStateGroup 的当前状态绑定(bind)到 UWP MVVM 应用中 View 模型中的属性

转载 作者:行者123 更新时间:2023-12-03 10:22:27 24 4
gpt4 key购买 nike

在带有 MVVM Light 的 UWP 应用程序中,我试图绑定(bind) VisualStateGroup 的当前状态到 ViewModel 中的一个属性。这个想法是通过根据应用程序逻辑更改 View 模型中属性的值来更改当前状态。

this question 中已经解决了这个问题。几年前。我对 Olav 建议的方法(第二个答案)感兴趣,因为我的应用程序是 UWP 应用程序,所以不能直接遵循。

这是我的xaml:

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
x:Class="Smallrobots.Ev3RemoteController.Views.Settings"
mc:Ignorable="d"

DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">

<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ConnectionStatus">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding VsmConnectionStatus}"
Value="{Binding VsmConnectionStatus}">
<Core:GoToStateAction StateName="{Binding VsmConnectionStatus}"/>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<VisualState x:Name="Connected">
<VisualState.Setters>
<Setter Target="button.(Control.IsEnabled)" Value="False"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<Button x:Name="button" Content="Connect"/>
</Grid>
</Page>

MainViewModel :

public class MainViewModel : ViewModelBase
{
private string vsmConnectionStatus = "";
public string VsmConnectionStatus
{
get => vsmConnectionStatus;
set
{
if (!vsmConnectionStatus.Equals(value))
{
vsmConnectionStatus = value;
RaisePropertyChanged("VsmConnectionStatus");
}
}
}

public MainViewModel()
{
VsmConnectionStatus = "Connected";
}
}

但是,使用此代码,在执行构造函数后,当前状态仍为 null并且进一步的分配没有效果。可能是使用 DataTriggerBehaviorGoToStateAction .

怎么了?非常感谢!

编辑:我在 xaml 中添加了更多代码,以更好地阐明问题。

最佳答案

However with this code, after the constructor has been executed, the current state remains null and further assignments have no effect. It could be the use of the DataTriggerBehavior or of the GoToStateAction.



由于我没有看到你的整个代码,我不确定你的其他代码块是否有问题。但是检查你上面的代码,你需要做一些改变。您需要移动 Interactivity:Interaction.BehaviorsVisualStateGroup如下所示:

<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ConnectionStatus">
<VisualState x:Name="Connected">
<VisualState.Setters>
<Setter Target="button.(Control.IsEnabled)" Value="False"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="NotConnected">
<VisualState.Setters>
<Setter Target="button.(Control.IsEnabled)" Value="True"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding VsmConnectionStatus}"
Value="{Binding VsmConnectionStatus}">
<Core:GoToStateAction StateName="{Binding VsmConnectionStatus}"/>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<Button Content="{Binding VsmConnectionStatus}" x:Name="button">
</Button>
</Grid>

二、如果要使用 Binding ,请确保您已为您的页面类指定了“DataContext”。

public MainPage()
{
this.InitializeComponent();
this.DataContext = new MainViewModel();
}

然后,根据我的测试,我发现在绑定(bind)值发生变化之前,不会触发“DataTriggerBehavior”。我不知道这是否是默认行为, GitHub 上也有同样的问题.

因此,您可以更改页面加载事件中的“VsmConnectionStatus”值,而不是在 viewmodel 的构造函数方法中进行更改。

例如,
public MainPage()
{
this.InitializeComponent();
this.DataContext = new MainViewModel();
this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
MainViewModel mvm = this.DataContext as MainViewModel;
mvm.VsmConnectionStatus = "Connected";
}

public class MainViewModel : ViewModelBase
{
private string vsmConnectionStatus = "";
public string VsmConnectionStatus
{
get => vsmConnectionStatus;
set
{
vsmConnectionStatus = value;
RaisePropertyChanged("VsmConnectionStatus");
}
}

public MainViewModel()
{}
}

关于c# - 将 VisualStateGroup 的当前状态绑定(bind)到 UWP MVVM 应用中 View 模型中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48289988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com