1

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

你能帮我找出我的代码中的错误吗?我无法将项目源分配给组合框以及 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();
    }
}
4

1 回答 1

1

对于 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 不再冻结。

于 2013-03-06T11:04:18.110 回答