c# - 在WinRT MVVM中无法执行按钮单击和组合框选择更改

标签 c# mvvm microsoft-metro windows-runtime windows-store-apps

更新1:您可以从here下载示例项目。

您能帮我在代码中找到错误吗?我无法将项目源分配给组合框以及WinRT应用程序中的按钮单击事件。我正在使用MVVM和MetroEventToCommand。我是MVVM概念的新手,所以请回答我的愚蠢问题。

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Click Here">
        <mvvm:EventToCommandManager.Collection>
            <mvvm:EventToCommand Command="{Binding ButtonClickCommand}" Event="Click"/>
        </mvvm:EventToCommandManager.Collection>
    </Button>

    <ComboBox x:Name="FontsCombo" Height="50" Width="150" SelectedItem="{Binding SelectedFont}"  ItemsSource="{Binding fonts}"  />

    <TextBlock FontSize="30" Text="{Binding SelectedFont}"/>
</Grid>

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

public class VM : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public RelayCommand ButtonClickCommand { get; set; }

    private ObservableCollection<string> _fonts = new ObservableCollection<string>();
    public ObservableCollection<string> fonts
    {
        get { return _fonts; }
        set
        {
            _fonts = value;
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("fonts"));
            }
        }
    }

    private string _SelectedFont = "";
    public string SelectedFont
    {
        get { return _SelectedFont; }
        set
        {
            // Some logic here
            _SelectedFont = value;
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedFont"));
            }

        }
    }
    public VM()
    {
        fonts.Add("Arial");
        fonts.Add("Courier New");
        fonts.Add("Times New Roman");
        ButtonClickCommand = new RelayCommand(Click);
    }

    private void Click()
    {
        new Action(async () => await new Windows.UI.Popups.MessageDialog("Testing dialog").ShowAsync()).Invoke();
    }
}

最佳答案

对于SelectedItem,您没有指定Mode = TwoWay:

<ComboBox x:Name="FontsCombo" Height="50" Width="150" SelectedItem="{Binding SelectedFont, Mode=TwoWay}"  ItemsSource="{Binding fonts}"  />

编辑
我找到了解决方案:
public class VM : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public RelayCommand ButtonClickCommand { get; set; }

    private ObservableCollection<string> _fonts;
    public ObservableCollection<string> fonts
    {
        get { return _fonts; }
        set
        {
            _fonts = value;
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("fonts"));
            }
        }
    }

    private string _SelectedFont;
    public string SelectedFont
    {
        get { return _SelectedFont; }
        set
        {
            // Some logic here
            _SelectedFont = value;
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedFont"));
            }

        }
    }
    public VM()
    {
        this.fonts = new ObservableCollection<string>();
        fonts.Add("Arial");
        fonts.Add("Courier New");
        fonts.Add("Times New Roman");
        ButtonClickCommand = new RelayCommand(Click);
    }

    private void Click()
    {
        new Action(async () => await new Windows.UI.Popups.MessageDialog("Testing dialog").ShowAsync()).Invoke();
    }
}

如果我在构造函数中实例化字体,则UX不再冻结。

关于c# - 在WinRT MVVM中无法执行按钮单击和组合框选择更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245526/

相关文章:

c# - 如何处理 View 模型中的 Slider.ValueChanged 事件?

c# - WPF MVVM Canvas ClickEvent

c# - 如何在 Windows 8 应用程序开发中添加磁贴图标?

css - 如何使用 CSS3 实现类似 Windows Phone 的 "tiled"3D 转换

c# - WPF 数据绑定(bind) - "Custom Type Descriptor"示例

c# - INSERT 语句冲突

c# - 如何将 Ninject 与 WebApi 一起使用?

c# - View 模型中的异步方法 : should this return Task or void?

c# - Caliburn Micro Xamarin 的数据绑定(bind)操作顺序

windows-8 - 检测 Windows 8 Store App 是否有触摸屏