C# Can';无法在WinRT MVVM中执行按钮单击和组合框选择更改

C# Can';无法在WinRT MVVM中执行按钮单击和组合框选择更改,c#,mvvm,microsoft-metro,windows-runtime,windows-store-apps,C#,Mvvm,Microsoft Metro,Windows Runtime,Windows Store Apps,更新1:您可以从下载示例项目 你能帮我找出代码中的错误吗。我无法在WinRT应用程序中将项目源分配给组合框以及按钮单击事件。我正在使用MVVM和。我不熟悉MVVM的概念,所以请回答我愚蠢的问题 <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Content="Click Here"> <mvvm:EventToCommandMan

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

你能帮我找出代码中的错误吗。我无法在WinRT应用程序中将项目源分配给组合框以及按钮单击事件。我正在使用MVVM和。我不熟悉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();
    }
}

公共主页()
{
this.InitializeComponent();
this.DataContext=new VM();
}
公共类VM:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共RelayCommand按钮ClickCommand{get;set;}
私有ObservableCollection_fonts=新ObservableCollection();
公共可观察收集字体
{
获取{return\u font;}
设置
{
_字体=值;
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“字体”);
}
}
}
私有字符串_SelectedFont=“”;
公共字符串选择字体
{
获取{return\u SelectedFont;}
设置
{
//这里有些逻辑
_SelectedFont=值;
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“SelectedFont”);
}
}
}
公共虚拟机()
{
字体。添加(“Arial”);
字体。添加(“快递新”);
字体。添加(“Times New Roman”);
ButtonClickCommand=新建中继命令(单击);
}
私有void Click()
{
新操作(async()=>等待新的Windows.UI.Popups.MessageDialog(“测试对话框”).ShowAsync()).Invoke();
}
}

对于SelectedItem,您没有指定模式=双向:

<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();
    }
}
公共类VM:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共RelayCommand按钮ClickCommand{get;set;}
私有可观察收集字体;
公共可观察收集字体
{
获取{return\u font;}
设置
{
_字体=值;
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“字体”);
}
}
}
私有字符串_SelectedFont;
公共字符串选择字体
{
获取{return\u SelectedFont;}
设置
{
//这里有些逻辑
_SelectedFont=值;
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“SelectedFont”);
}
}
}
公共虚拟机()
{
this.fonts=新的ObservableCollection();
字体。添加(“Arial”);
字体。添加(“快递新”);
字体。添加(“Times New Roman”);
ButtonClickCommand=新建中继命令(单击);
}
私有void Click()
{
新操作(async()=>等待新的Windows.UI.Popups.MessageDialog(“测试对话框”).ShowAsync()).Invoke();
}
}

如果我在构造函数中引用字体,UX就不再冻结

对于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();
    }
}
公共类VM:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共RelayCommand按钮ClickCommand{get;set;}
私有可观察收集字体;
公共可观察收集字体
{
获取{return\u font;}
设置
{
_字体=值;
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“字体”);
}
}
}
私有字符串_SelectedFont;
公共字符串选择字体
{
获取{return\u SelectedFont;}
设置
{
//这里有些逻辑
_SelectedFont=值;
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“SelectedFont”);
}
}
}
公共虚拟机()
{
this.fonts=新的ObservableCollection();
字体。添加(“Arial”);
字体。添加(“快递新”);
字体。添加(“Times New Roman”);
ButtonClickCommand=新建中继命令(单击);
}
私有void Click()
{
新操作(async()=>等待新的Windows.UI.Popups.MessageDialog(“测试对话框”).ShowAsync()).Invoke();
}
}

如果我在构造函数中引用字体,UX就不再冻结

combobox的事件项资源不工作?你能在这里发布所有MainPage.xaml吗?顺便说一句:新操作(异步()=>等待新的Windows.UI.Popups.MessageDialog(“测试对话框”).ShowAsync()).Invoke();--这让我大吃一惊,我的组合框无法获取项目资源,按钮点击也无法工作。请参阅示例事件项combobox的源不工作?你能在这里发布所有MainPage.xaml吗?顺便说一句:新操作(异步()=>等待新的Windows.UI.Popups.MessageDialog(“测试对话框”).ShowAsync()).Invoke();--这让我大吃一惊,我的组合框无法获取项目资源,按钮点击也无法工作。请看样品不工作,我已经上传了我的请看当我尝试你的项目,用户体验是冻结。它也在您的项目中吗?是的,我无法单击按钮,也无法打开组合框仍然无法工作:(参见“我忘了告诉你:用StackPanel替换网格!”在你的项目中,文本块位于网格和按钮上。因此你可以使用它背后的控件。不工作,我已上载我的。请看一看当我尝试你的项目时,UX已冻结。它也在你的项目中吗?是的,我无法单击按钮,也无法打开组合框仍然不工作金:(看我忘了告诉你的:用StackPanel替换网格!在你的项目中,Textblock在Grid&按钮上。所以你可以使用contr