diff --git a/GoogleCast.SampleApp/App.xaml.cs b/GoogleCast.SampleApp/App.xaml.cs index e84852e..13b880d 100644 --- a/GoogleCast.SampleApp/App.xaml.cs +++ b/GoogleCast.SampleApp/App.xaml.cs @@ -1,5 +1,6 @@ -using GalaSoft.MvvmLight.Threading; -using System.Windows; +using System.Windows; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Toolkit.Mvvm.DependencyInjection; namespace GoogleCast.SampleApp { @@ -10,7 +11,12 @@ public partial class App : Application { static App() { - DispatcherHelper.Initialize(); + var services = new ServiceCollection(); + services.AddGoogleCast(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + Ioc.Default.ConfigureServices(services.BuildServiceProvider()); } } } diff --git a/GoogleCast.SampleApp/GoogleCast.SampleApp.csproj b/GoogleCast.SampleApp/GoogleCast.SampleApp.csproj index dd277b0..beedc2f 100644 --- a/GoogleCast.SampleApp/GoogleCast.SampleApp.csproj +++ b/GoogleCast.SampleApp/GoogleCast.SampleApp.csproj @@ -1,8 +1,8 @@ - + {347B2A0B-04B1-40D7-A63E-D0077A651306} WinExe - net461 + net5.0-windows latest enable {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} @@ -25,12 +25,7 @@ pdbonly - - - - - - + diff --git a/GoogleCast.SampleApp/MainViewModel.cs b/GoogleCast.SampleApp/MainViewModel.cs index 47d83f0..ddf1b85 100644 --- a/GoogleCast.SampleApp/MainViewModel.cs +++ b/GoogleCast.SampleApp/MainViewModel.cs @@ -3,18 +3,17 @@ using System.Drawing; using System.Linq; using System.Threading.Tasks; -using GalaSoft.MvvmLight; -using GalaSoft.MvvmLight.Command; -using GalaSoft.MvvmLight.Threading; using GoogleCast.Channels; using GoogleCast.Models.Media; +using Microsoft.Toolkit.Mvvm.ComponentModel; +using Microsoft.Toolkit.Mvvm.Input; namespace GoogleCast.SampleApp { /// /// Main view model /// - class MainViewModel : ViewModelBase + public class MainViewModel : ObservableObject { /// /// Initializes a new instance of class @@ -32,11 +31,6 @@ public MainViewModel(IDeviceLocator deviceLocator, ISender sender) PauseCommand = new RelayCommand(async () => await TryAsync(PauseAsync), () => AreButtonsEnabled); StopCommand = new RelayCommand(async () => await TryAsync(StopAsync), () => AreButtonsEnabled); RefreshCommand = new RelayCommand(async () => await TryAsync(RefreshAsync), () => IsLoaded); - - if (!IsInDesignMode) - { - Task.Run(RefreshAsync); - } } private IDeviceLocator DeviceLocator { get; } @@ -49,7 +43,7 @@ public MainViewModel(IDeviceLocator deviceLocator, ISender sender) public IEnumerable? Receivers { get => _receivers; - private set => Set(nameof(Receivers), ref _receivers, value); + private set => SetProperty(ref _receivers, value); } private bool IsInitialized { get; set; } @@ -68,8 +62,8 @@ public IReceiver? SelectedReceiver { _selectedReceiver = value; IsInitialized = false; - RaisePropertyChanged(nameof(SelectedReceiver)); - RaiseButtonsCommandsCanExecuteChanged(); + OnPropertyChanged(nameof(SelectedReceiver)); + NotifyButtonsCommandsCanExecuteChanged(); } } } @@ -86,9 +80,9 @@ private set if (_isLoaded != value) { _isLoaded = value; - RaisePropertyChanged(nameof(IsLoaded)); - RefreshCommand.RaiseCanExecuteChanged(); - RaiseButtonsCommandsCanExecuteChanged(); + OnPropertyChanged(nameof(IsLoaded)); + RefreshCommand.NotifyCanExecuteChanged(); + NotifyButtonsCommandsCanExecuteChanged(); } } } @@ -105,7 +99,7 @@ private set public string? PlayerState { get => _playerState; - private set => Set(nameof(PlayerState), ref _playerState, value); + private set => SetProperty(ref _playerState, value); } private string _link = "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/DesigningForGoogleCast.mp4"; @@ -121,8 +115,8 @@ public string Link { _link = value; IsInitialized = false; - RaisePropertyChanged(nameof(Link)); - RaiseButtonsCommandsCanExecuteChanged(); + OnPropertyChanged(nameof(Link)); + NotifyButtonsCommandsCanExecuteChanged(); } } } @@ -134,7 +128,7 @@ public string Link public string Subtitle { get => _subtitle; - set => Set(nameof(Subtitle), ref _subtitle, value); + set => SetProperty(ref _subtitle, value); } private bool _isMuted; @@ -149,13 +143,13 @@ public bool IsMuted if (_isMuted != value) { _isMuted = value; - RaisePropertyChanged(nameof(IsMuted)); + OnPropertyChanged(nameof(IsMuted)); Task.Run(SetIsMutedAsync); } } } - public float _volume = 1; + private float _volume = 1; /// /// Gets or sets the volume /// @@ -171,7 +165,7 @@ public float Volume IsMuted = false; } _volume = value; - RaisePropertyChanged(nameof(Volume)); + OnPropertyChanged(nameof(Volume)); Task.Run(SetVolumeAsync); } } @@ -203,12 +197,12 @@ private bool IsStopped /// public RelayCommand RefreshCommand { get; } - private void RaiseButtonsCommandsCanExecuteChanged() + private void NotifyButtonsCommandsCanExecuteChanged() { - RaisePropertyChanged(nameof(AreButtonsEnabled)); - PlayCommand.RaiseCanExecuteChanged(); - PauseCommand.RaiseCanExecuteChanged(); - StopCommand.RaiseCanExecuteChanged(); + OnPropertyChanged(nameof(AreButtonsEnabled)); + PlayCommand.NotifyCanExecuteChanged(); + PauseCommand.NotifyCanExecuteChanged(); + StopCommand.NotifyCanExecuteChanged(); } private async Task TryAsync(Func action) @@ -305,15 +299,16 @@ private async Task StopAsync() } } - private async Task RefreshAsync() + /// + /// Finds the receivers + /// + /// a that represents the asynchronous operation + public async Task RefreshAsync() { - await DispatcherHelper.RunAsync(async () => - { - IsLoaded = false; - Receivers = await DeviceLocator.FindReceiversAsync(); - SelectedReceiver = Receivers.FirstOrDefault(); - IsLoaded = true; - }); + IsLoaded = false; + Receivers = await DeviceLocator.FindReceiversAsync(); + SelectedReceiver = Receivers.FirstOrDefault(); + IsLoaded = true; } private async Task SetVolumeAsync() @@ -326,9 +321,9 @@ private async Task SetIsMutedAsync() await SendChannelCommandAsync(IsStopped, null, async c => await c.SetIsMutedAsync(IsMuted)); } - private void MediaChannelStatusChanged(object sender, EventArgs e) + private void MediaChannelStatusChanged(object? sender, EventArgs e) { - var status = ((IMediaChannel)sender).Status?.FirstOrDefault(); + var status = (sender as IMediaChannel)?.Status?.FirstOrDefault(); var playerState = status?.PlayerState; if (playerState == "IDLE" && !string.IsNullOrEmpty(status!.IdleReason)) { @@ -337,11 +332,11 @@ private void MediaChannelStatusChanged(object sender, EventArgs e) PlayerState = playerState; } - private void ReceiverChannelStatusChanged(object sender, EventArgs e) + private void ReceiverChannelStatusChanged(object? sender, EventArgs e) { if (!IsInitialized) { - var status = ((IReceiverChannel)sender).Status; + var status = (sender as IReceiverChannel)?.Status; if (status != null) { if (status.Volume.Level != null) diff --git a/GoogleCast.SampleApp/MainWindow.xaml b/GoogleCast.SampleApp/MainWindow.xaml index 7a092d4..6b00c0c 100644 --- a/GoogleCast.SampleApp/MainWindow.xaml +++ b/GoogleCast.SampleApp/MainWindow.xaml @@ -6,6 +6,7 @@ mc:Ignorable="d" Title="GoogleCast" Height="200" Width="784" ResizeMode="CanResizeWithGrip" + Loaded="WindowLoadedAsync" DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"> @@ -57,4 +58,4 @@ - \ No newline at end of file + diff --git a/GoogleCast.SampleApp/MainWindow.xaml.cs b/GoogleCast.SampleApp/MainWindow.xaml.cs index 3bf0e8d..62b7315 100644 --- a/GoogleCast.SampleApp/MainWindow.xaml.cs +++ b/GoogleCast.SampleApp/MainWindow.xaml.cs @@ -1,4 +1,5 @@ -using System.Windows; +using System.ComponentModel; +using System.Windows; namespace GoogleCast.SampleApp { @@ -14,5 +15,15 @@ public MainWindow() { InitializeComponent(); } + + private new MainViewModel DataContext => (MainViewModel)base.DataContext; + + private async void WindowLoadedAsync(object sender, RoutedEventArgs e) + { + if (!DesignerProperties.GetIsInDesignMode(this)) + { + await DataContext.RefreshAsync(); + } + } } -} \ No newline at end of file +} diff --git a/GoogleCast.SampleApp/ViewModelLocator.cs b/GoogleCast.SampleApp/ViewModelLocator.cs index a8896b5..0bb7d75 100644 --- a/GoogleCast.SampleApp/ViewModelLocator.cs +++ b/GoogleCast.SampleApp/ViewModelLocator.cs @@ -1,23 +1,12 @@ -using CommonServiceLocator; -using GalaSoft.MvvmLight.Ioc; +using Microsoft.Toolkit.Mvvm.DependencyInjection; namespace GoogleCast.SampleApp { class ViewModelLocator { - static ViewModelLocator() - { - var simpleIoc = SimpleIoc.Default; - simpleIoc.Reset(); - ServiceLocator.SetLocatorProvider(() => simpleIoc); - simpleIoc.Register(); - simpleIoc.Register(() => new Sender()); - simpleIoc.Register(); - } - - public MainViewModel Main - { - get { return ServiceLocator.Current.GetInstance(); } - } + /// + /// Gets the main view model + /// + public static MainViewModel Main => Ioc.Default.GetRequiredService(); } } diff --git a/GoogleCast.sln b/GoogleCast.sln index f58194f..37a3a45 100644 --- a/GoogleCast.sln +++ b/GoogleCast.sln @@ -5,11 +5,12 @@ VisualStudioVersion = 16.0.31205.134 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GoogleCast", "GoogleCast\GoogleCast.csproj", "{0B336E66-C5C4-47B7-BD45-85847CCEF991}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleCast.SampleApp", "GoogleCast.SampleApp\GoogleCast.SampleApp.csproj", "{347B2A0B-04B1-40D7-A63E-D0077A651306}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GoogleCast.SampleApp", "GoogleCast.SampleApp\GoogleCast.SampleApp.csproj", "{347B2A0B-04B1-40D7-A63E-D0077A651306}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DA711E96-61F5-4F90-8E8A-54AB95FC155D}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig + .github\workflows\dotnet.yml = .github\workflows\dotnet.yml EndProjectSection EndProject Global