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 に答える