Skip to content

Commit

Permalink
Added example in TestApplication to handle two instances of the same …
Browse files Browse the repository at this point in the history
…view, ensuring correct toast retrieval per instance.
  • Loading branch information
V4SS3UR committed Sep 26, 2024
1 parent c20c411 commit 6e4dfc0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
7 changes: 5 additions & 2 deletions TestApplication/MVVM/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Button Content="Info" Command="{Binding InfoButtonCommand}"/>
<Button Content="CustomToast" Command="{Binding CustomToastButtonCommand}"/>
<Button Content="CustomView" Command="{Binding CustomViewButtonCommand}"/>
<Button Content="OpenNewApp" Command="{Binding OpenNewInstanceCommand}"/>
</UniformGrid>

<UniformGrid Rows="1">
Expand All @@ -56,11 +57,13 @@
<Border Background="#3000" CornerRadius="5" Margin="5">
<local:View2 Margin="5" Background="#8293A7"/>
</Border>

<Border Background="#3000" CornerRadius="5" Margin="5">
<local:View2 Margin="5" Background="#8293A7"/>
</Border>
</UniformGrid>
</DockPanel>

<Border Background="White"/>

<toast:Toast ToastName="MainWindowToast" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

</Grid>
Expand Down
26 changes: 17 additions & 9 deletions TestApplication/MVVM/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public partial class MainWindow : Window
public RelayCommand InfoButtonCommand { get; set; }
public RelayCommand CustomToastButtonCommand { get; set; }
public RelayCommand CustomViewButtonCommand { get; set; }
public RelayCommand OpenNewInstanceCommand { get; set; }

private Toast toast
{
{
get => Toast.GetToast("MainWindowToast");
}

Expand All @@ -38,8 +39,6 @@ public MainWindow()
InitializeComponent();
this.DataContext = this;

var toast = Toast.GetToast("MainWindowToast");

WarningButtonCommand = new RelayCommand((parameter) =>
{
toast.ShowWarningToast("This is a warning toast", darkOverlay: DarkOverlay);
Expand All @@ -64,17 +63,26 @@ public MainWindow()
{
ShowCustomView();
});
OpenNewInstanceCommand = new RelayCommand((parameter) =>
{
var window = new MainWindow();
window.Show();
});

this.Loaded += MainWindow_Loaded;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//Bring a welcome message when first loaded
var message = "Hi, welcome to ToastManager!";
var title = "Information";


this.Loaded += (sender, e) =>
try
{
//Bring a welcome message when first loaded
var message = "Hi, welcome to ToastManager!";
var title = "Information";
toast.ShowInfoToast(message, title, darkOverlay: true);
};
}
catch { }
}

private void ShowCustomToast()
Expand Down
3 changes: 1 addition & 2 deletions TestApplication/MVVM/View/View2.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<Button Content="Info" Command="{Binding InfoButtonCommand}"/>
</UniformGrid>


<toast:Toast ToastName="View2Toast" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<toast:Toast ToastName="{Binding ToastNameWithGuid, RelativeSource={RelativeSource AncestorType={x:Type local:View2}}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</UserControl>
15 changes: 13 additions & 2 deletions TestApplication/MVVM/View/View2.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Specialized;
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
Expand All @@ -17,9 +18,16 @@ public partial class View2 : UserControl, INotifyPropertyChanged
public RelayCommand SuccessButtonCommand { get; set; }
public RelayCommand InfoButtonCommand { get; set; }

private string _toastNameWithGuid; public string ToastNameWithGuid
{
get { return _toastNameWithGuid; }
set { _toastNameWithGuid = value; OnPropertyChanged(); }
}


private Toast toast
{
get => Toast.GetToast("View2Toast");
get => Toast.GetToast(this.ToastNameWithGuid);
}

private bool _darkOverlay; public bool DarkOverlay
Expand All @@ -33,6 +41,9 @@ public View2()
InitializeComponent();
this.DataContext = this;

var shortGuid = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
ToastNameWithGuid = "View2Toast" + shortGuid;

DarkOverlay = true;

WarningButtonCommand = new RelayCommand((parameter) =>
Expand Down

0 comments on commit 6e4dfc0

Please sign in to comment.