Skip to content

Commit

Permalink
Migrate to CommunityToolkit.Mvvm (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Nov 15, 2024
1 parent da33fae commit fc048fe
Show file tree
Hide file tree
Showing 31 changed files with 830 additions and 678 deletions.
6 changes: 6 additions & 0 deletions README-BEFORE-UPDATE-REFS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Notes on updating the project references:

IMPORTANT VERSION CONSTRAINTS:
- System.Threading.Tasks.Extensions must not be updated beyond version 4.5.4
Reason: The VS2019 extension will fail due to a transitive dependency conflict between
Compilers.Services.Unsafe and System.Collections.Immutable.
14 changes: 7 additions & 7 deletions USB Test App WPF/App.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Application x:Class="Serial_Test_App_WPF.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Serial_Test_App_WPF" StartupUri="MainWindow.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:Serial_Test_App_WPF.ViewModel" />
</ResourceDictionary>
</Application.Resources>
</Application>
<Application x:Class="Serial_Test_App_WPF.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Serial_Test_App_WPF" StartupUri="MainWindow.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:Serial_Test_App_WPF.ViewModel"/>
</ResourceDictionary>
</Application.Resources>
</Application>
9 changes: 7 additions & 2 deletions USB Test App WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using CommonServiceLocator;
using nanoFramework.ANT.Services.NanoFrameworkService;
using nanoFramework.Tools.Debugger;
using Serial_Test_App_WPF.ViewModel;
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.DependencyInjection;

namespace Serial_Test_App_WPF
{
Expand All @@ -27,12 +32,12 @@ public App()
vml = new ViewModelLocator();

var serialClient = CreateSerialDebugClient();
ServiceLocator.Current.GetInstance<MainViewModel>().SerialDebugService = serialClient;
Ioc.Default.GetService<MainViewModel>().SerialDebugService = serialClient;
}

private void App_Exit(object sender, System.Windows.ExitEventArgs e)
{
var serialClient = ServiceLocator.Current.GetInstance<MainViewModel>().SerialDebugService;
var serialClient = Ioc.Default.GetService<MainViewModel>().SerialDebugService;
serialClient.SerialDebugClient.StopDeviceWatchers();
}

Expand Down
4 changes: 0 additions & 4 deletions USB Test App WPF/FodyWeavers.xml

This file was deleted.

54 changes: 0 additions & 54 deletions USB Test App WPF/FodyWeavers.xsd

This file was deleted.

5 changes: 2 additions & 3 deletions USB Test App WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Serial_Test_App_WPF"
xmlns:ViewModel="clr-namespace:Serial_Test_App_WPF.ViewModel" x:Class="Serial_Test_App_WPF.MainWindow"
xmlns:ViewModel="clr-namespace:Serial_Test_App_WPF.ViewModel"
x:Class="Serial_Test_App_WPF.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="374" Width="570.729">
<Window.Resources>
Expand Down Expand Up @@ -54,7 +55,5 @@
<Button Content="Reboot CLR" HorizontalAlignment="Left" Margin="364,77,0,0" VerticalAlignment="Top" Width="121" Click="RebootDeviceButton_Click"/>
<Button Content="Upload File I:\" HorizontalAlignment="Left" Margin="39,306,0,0" VerticalAlignment="Top" Width="110" Click="UploadFileInternalStorage_Click"/>
<Button Content="Rm file I:\" HorizontalAlignment="Left" Margin="162,306,0,0" VerticalAlignment="Top" Width="75" Click="RemoveFileInternalStorage_Click"/>


</Grid>
</Window>
2 changes: 1 addition & 1 deletion USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ private void SetDeviceConfigButton_Click(object sender, RoutedEventArgs e)
Password = "1234",
Authentication = AuthenticationType.WPA2,
Encryption = EncryptionType.WPA2,
Options = Wireless80211_ConfigurationOptions.AutoConnect
Wireless80211Options = Wireless80211_ConfigurationOptions.AutoConnect
};

// write wireless configuration to device
Expand Down
16 changes: 7 additions & 9 deletions USB Test App WPF/Serial Test App WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,20 @@
<PackageReference Include="CommonServiceLocator">
<Version>2.0.7</Version>
</PackageReference>
<PackageReference Include="Fody">
<Version>4.2.1</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>8.3.2</Version>
</PackageReference>
<PackageReference Include="MvvmLight">
<Version>5.4.1.1</Version>
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>6.0.2</Version>
</PackageReference>
<PackageReference Include="nanoFramework.CoreLibrary">
<Version>1.14.2</Version>
</PackageReference>
<PackageReference Include="PropertyChanged.Fody">
<Version>2.6.1</Version>
<PackageReference Include="System.IO.Ports">
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="System.Threading.Tasks.Extensions">
<Version>4.5.4</Version>
<Version>4.6.0</Version>
</PackageReference>
<PackageReference Include="System.ValueTuple">
<Version>4.5.0</Version>
Expand Down
77 changes: 41 additions & 36 deletions USB Test App WPF/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using GalaSoft.MvvmLight;
using nanoFramework.ANT.Services.NanoFrameworkService;
using nanoFramework.Tools.Debugger;
using nanoFramework.Tools.Debugger.WireProtocol;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -16,6 +14,7 @@
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;

namespace Serial_Test_App_WPF.ViewModel
{
Expand All @@ -31,12 +30,15 @@ namespace Serial_Test_App_WPF.ViewModel
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
[AddINotifyPropertyChangedInterface]
public class MainViewModel : ViewModelBase
public class MainViewModel : ObservableObject
{
private readonly object _lockObj = new object();

INFSerialDebugClientService _serialDebugService;
private INFSerialDebugClientService _serialDebugService;
private ObservableCollection<NanoDeviceBase> _availableDevices;
private NanoDeviceBase _selectedDevice;
private List<TransportType> _availableTransportTypes;
private TransportType _selectedTransportType;

/// <summary>
/// Initializes a new instance of the MainViewModel class.
Expand All @@ -53,34 +55,24 @@ public MainViewModel()
////}
}

public INFSerialDebugClientService SerialDebugService
{
get
{
return _serialDebugService;
}

public INFSerialDebugClientService SerialDebugService
{
get => _serialDebugService;
set
{
if (_serialDebugService == value)
if (SetProperty(ref _serialDebugService, value))
{
return;
}
SelectedTransportType = TransportType.Serial;

_serialDebugService = value;
AvailableDevices = _serialDebugService.SerialDebugClient.NanoFrameworkDevices;

RaisePropertyChanged("SerialDebugService");
// need to do this in order to allow sync from another thread
BindingOperations.EnableCollectionSynchronization(AvailableDevices, _lockObj);

SelectedTransportType = TransportType.Serial;
SerialDebugService.SerialDebugClient.NanoFrameworkDevices.CollectionChanged += NanoFrameworkDevices_CollectionChanged;

AvailableDevices = _serialDebugService.SerialDebugClient.NanoFrameworkDevices;

// need to do this in order to allow sync from another thread
BindingOperations.EnableCollectionSynchronization(AvailableDevices, _lockObj);

SerialDebugService.SerialDebugClient.NanoFrameworkDevices.CollectionChanged += NanoFrameworkDevices_CollectionChanged;

SerialDebugService.SerialDebugClient.LogMessageAvailable += SerialDebugClient_LogMessageAvailable1;
SerialDebugService.SerialDebugClient.LogMessageAvailable += SerialDebugClient_LogMessageAvailable1;
}
}
}

Expand All @@ -95,7 +87,7 @@ private void SerialDebugClient_LogMessageAvailable1(object sender, StringEventAr
}
catch
{
// catch all as the dispatcher is ont always available and that's OK
// catch all as the dispatcher is not always available and that's OK
}
}

Expand All @@ -106,7 +98,11 @@ private void SerialDebugClient_LogMessageAvailable(object sender, StringEventArg
}));
}

public ObservableCollection<NanoDeviceBase> AvailableDevices { get; set; }
public ObservableCollection<NanoDeviceBase> AvailableDevices
{
get => _availableDevices;
set => SetProperty(ref _availableDevices, value);
}

private void NanoFrameworkDevices_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Expand All @@ -119,7 +115,6 @@ private void NanoFrameworkDevices_CollectionChanged(object sender, System.Collec
// break;

case TransportType.Serial:

break;

default:
Expand All @@ -129,17 +124,27 @@ private void NanoFrameworkDevices_CollectionChanged(object sender, System.Collec

// if there's just one, select it
SelectedDevice = (AvailableDevices.Count == 1) ? AvailableDevices.First() : null;

}));
}

public NanoDeviceBase SelectedDevice { get; set; }
public NanoDeviceBase SelectedDevice
{
get => _selectedDevice;
set => SetProperty(ref _selectedDevice, value);
}

#region Transport
public List<TransportType> AvailableTransportTypes { get; set; }

public TransportType SelectedTransportType { get; set; }
public List<TransportType> AvailableTransportTypes
{
get => _availableTransportTypes;
set => SetProperty(ref _availableTransportTypes, value);
}

public TransportType SelectedTransportType
{
get => _selectedTransportType;
set => SetProperty(ref _selectedTransportType, value);
}
#endregion
}
}
}
Loading

0 comments on commit fc048fe

Please sign in to comment.