CommunityToolkit / MVVM-Samples

Sample repo for MVVM package
Other
1.15k stars 224 forks source link

WPF Samples #67

Open shaggygi opened 2 years ago

shaggygi commented 2 years ago

Anyone have a simple WPF (full framework) sample working with the MVVM toolkit? I've tried following the docs, but not having much luck.

Designer is not loading saying build is needed to load, unable to cast object of type "WpfSurfaceApp". Binding a TextBlock and Button and appear to run/work, but designer is not happy.

Prob something I'm doing wrong. Thx

avsteele commented 2 years ago

Here something quick I wrote to test out the ObservableProperty, NotifyPropertyChangedFor, and RelayCommand (with cancellation) attributes.

Designer seems to load OK, but it does not appear to pick up on some things; if you specify a DataContext in the designer it it does pick up on Properties generated by the ObservableProperty attribute, but does not pick up those generated by RelayCommand attribute.

MainWindow.cs:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

using System.ComponentModel;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Community8Test
{

    [INotifyPropertyChanged]
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            System.Timers.Timer timer = new System.Timers.Timer(1000);
            timer.Elapsed += Timer_Elapsed;
            timer.AutoReset = true;
            timer.Start();
        }

        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            Counter = (Counter + 100)%3000;
        }

        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(NegCounter))]
        private int counter;

        public int NegCounter => -counter;

        [RelayCommand(IncludeCancelCommand = true)]
        private async Task DoThingAsync(CancellationToken token=default)
        {
            try
            {
                await Task.Delay(Counter, token);
            } catch (OperationCanceledException) { }
        }

    }
}

MainWindow.xaml:

<Window x:Class="Community8Test.MainWindow"
        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:local="clr-namespace:Community8Test"
        mc:Ignorable="d"
        Title="MainWindow"
        SizeToContent="WidthAndHeight"
        d:DataContext="{d:DesignInstance local:MainWindow}"
        >
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="24"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="{Binding Counter}"/>
        <TextBlock Text="{Binding NegCounter}"/>
        <Button Content="DoAsync" Command="{Binding DoThingCommand}"/>
        <Button Content="Cancel" Command="{Binding DoThingCancelCommand}"/>
    </StackPanel>
</Window>