WPF GeckoBrowser Not able to load url in the windows browser
I am using gecko web browser for my WPF windows application. We have a particular URL which is opening in all the system browser but not working on the windows browser.But we use any other it loads. When contacted the support team they were saying need to enable javascript. So please help me how to enable the javascript in gecko browser.
The URL load is the partial load. we are getting the background related UI for the URL. The body part of the url is not loading
I have used inbuilt .net browser but that URL is not loading in that application also.
If you want to use WebView2 in your project you can do the following:
Add nuget package Microsoft.Web.WebView2
Create WPF view with WebView2:
<wpf:WebView2 x:Name="WebView2">
<i:Interaction.Behaviors>
<behaviors:WebView2NavigateBehavior Url="{Binding Url}" RefreshInterval="{Binding RefreshInterval}" />
</i:Interaction.Behaviors>
</wpf:WebView2>
With code behind:
public partial class BrowserView : IDisposable
{
private bool disposed;
static BrowserView()
{
string loaderPath = ServiceLocator.Current.Resolve<IPathResolver>().GetWebView2LoaderDllDirectory(RuntimeInformation.ProcessArchitecture);
CoreWebView2Environment.SetLoaderDllFolderPath(loaderPath);
}
public BrowserView()
{
this.InitializeComponent();
this.InitializeAsync();
}
private async void InitializeAsync()
{
try
{
await this.WebView2.EnsureCoreWebView2Async();
}
catch (Exception ex)
{
//Log exception here
}
}
public void Dispose()
{
if (!this.disposed)
{
this.WebView2?.Dispose();
this.disposed = true;
}
}
}
Here's the code for view behavior:
public sealed class WebView2NavigateBehavior : BehaviorBase<WebView2>
{
public static readonly DependencyProperty UrlProperty =
DependencyProperty.Register(nameof(Url), typeof(WebsiteUrl), typeof(WebView2NavigateBehavior),
new PropertyMetadata(default(WebsiteUrl), PropertyChangedCallback));
public static readonly DependencyProperty RefreshIntervalProperty =
DependencyProperty.Register(nameof(RefreshInterval), typeof(TimeSpan), typeof(WebView2NavigateBehavior),
new PropertyMetadata(default(TimeSpan), PropertyChangedCallback));
private DispatcherTimer? timer;
public WebsiteUrl? Url
{
get => (WebsiteUrl?)this.GetValue(UrlProperty);
set => this.SetValue(UrlProperty, value);
}
public TimeSpan RefreshInterval
{
get => (TimeSpan)this.GetValue(RefreshIntervalProperty);
set => this.SetValue(RefreshIntervalProperty, value);
}
protected override void OnSetup()
{
base.OnSetup();
this.AssociatedObject.CoreWebView2InitializationCompleted += this.OnCoreWebView2InitializationCompleted;
}
protected override void OnCleanup()
{
base.OnCleanup();
this.StopRefresh();
}
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = (WebView2NavigateBehavior)d;
if (e.Property == UrlProperty && e.NewValue is WebsiteUrl url)
behavior.Navigate(url);
else if (e.Property == RefreshIntervalProperty && e.NewValue is TimeSpan interval)
{
behavior.StopRefresh();
if (interval != TimeSpan.Zero)
behavior.StartRefresh(interval);
}
}
private void Navigate(WebsiteUrl? url)
{
if (this.AssociatedObject.IsInitialized && this.AssociatedObject.CoreWebView2 != null && url != null)
this.AssociatedObject.CoreWebView2.Navigate(url.ToString());
}
private void OnCoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
this.AssociatedObject.CoreWebView2InitializationCompleted -= this.OnCoreWebView2InitializationCompleted;
if (e.IsSuccess)
this.Navigate(this.Url);
}
private void StartRefresh(TimeSpan interval)
{
this.timer = new DispatcherTimer { Interval = interval };
this.timer.Tick += this.OnTick;
this.timer.Start();
}
private void StopRefresh()
{
if (this.timer != null)
{
this.timer.Stop();
this.timer.Tick -= this.OnTick;
}
this.timer = null;
}
private void OnTick(object sender, EventArgs e)
{
if (this.AssociatedObject.IsInitialized)
this.AssociatedObject.CoreWebView2?.Reload();
}
}
The code for the ViewModel:
public class BrowserViewModel : ViewModelBase<BrowserViewModel>
{
private WebsiteUrl? url;
private string? title;
private TimeSpan refreshInterval;
public WebsiteUrl? Url
{
get => this.url;
set => this.SetProperty(ref this.url, value);
}
public string? Title
{
get => this.title;
set => this.SetProperty(ref this.title, value);
}
public TimeSpan RefreshInterval
{
get => this.refreshInterval;
set => this.SetProperty(ref this.refreshInterval, value);
}
}
Related
We have setup some Xamarin behavior for not null entry fields etc, this fires when the user makes a change to a field and we then changed the entry border color, red for invalid.
However, we'd also like to reuse this behaviors when a submit button is tapped.
So I need to fire the TextChanged event manually, any ideas how I can do this, now sure if it's possible ?
public class NotEmptyEntryBehaviour : Behavior<Entry>
{
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += OnEntryTextChanged;
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= OnEntryTextChanged;
base.OnDetachingFrom(bindable);
}
void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
if (args == null)
return;
var oldString = args.OldTextValue;
var newString = args.NewTextValue;
}
}
If you want an alternative you can use one of the pre-built validation behaviors that comes with Xamarin.CommunityToolkit package, like TextValidationBehavior (by specifying a Regexp) or any more specific derived ones (example NumericValidationBehavior) that may fit your needs or even create a custom one by sub-classing ValidationBehavior.
It let you define custom styles for Valid and InValid states, but more important for the question has an async method called ForceValidate().
Also the Flags property could be interesting.
NotEmptyEntryBehaviour seems closer to TextValidationBehavior with MinimumLenght=1
xaml
<Entry Placeholder="Type something..." x:Name="entry">
<Entry.Behaviors>
<xct:TextValidationBehavior Flags="ValidateOnValueChanging"
InvalidStyle="{StaticResource InvalidEntryStyle}"
ValidStyle="{StaticResource ValidEntryStyle}"/>
</Entry.Behaviors>
</Entry>
Code
await (entry.Behaviors[0] as TextValidationBehavior)?.ForceValidate();
Docs
https://learn.microsoft.com/en-us/xamarin/community-toolkit/behaviors/charactersvalidationbehavior
Repo Samples
https://github.com/xamarin/XamarinCommunityToolkit/tree/main/samples/XCT.Sample/Pages/Behaviors
EDIT
If you want to run the validation from the ViewModel you need to bind ForceValidateCommand as explained in this GitHub discussion/question.
We have setup some Xamarin behavior for not null entry fields etc, this fires when the user makes a change to a field and we then changed the entry border color, red for invalid.
You can create custom Entry with behavior to get.
The first I’m going to do is to create a new control that inherits from Entry and will add three properties: IsBorderErrorVisible, BorderErrorColor, ErrorText.
public class ExtendedEntry : Entry
{
public static readonly BindableProperty IsBorderErrorVisibleProperty =
BindableProperty.Create(nameof(IsBorderErrorVisible), typeof(bool), typeof(ExtendedEntry), false, BindingMode.TwoWay);
public bool IsBorderErrorVisible
{
get { return (bool)GetValue(IsBorderErrorVisibleProperty); }
set
{
SetValue(IsBorderErrorVisibleProperty, value);
}
}
public static readonly BindableProperty BorderErrorColorProperty =
BindableProperty.Create(nameof(BorderErrorColor), typeof(Xamarin.Forms.Color), typeof(ExtendedEntry), Xamarin.Forms.Color.Transparent, BindingMode.TwoWay);
public Xamarin.Forms.Color BorderErrorColor
{
get { return (Xamarin.Forms.Color)GetValue(BorderErrorColorProperty); }
set
{
SetValue(BorderErrorColorProperty, value);
}
}
public static readonly BindableProperty ErrorTextProperty =
BindableProperty.Create(nameof(ErrorText), typeof(string), typeof(ExtendedEntry), string.Empty);
public string ErrorText
{
get { return (string)GetValue(ErrorTextProperty); }
set
{
SetValue(ErrorTextProperty, value);
}
}
}
Then creating custom render to android platform.
[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRenderer))]
namespace FormsSample.Droid
{
public class ExtendedEntryRenderer : EntryRenderer
{
public ExtendedEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null) return;
UpdateBorders();
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Control == null) return;
if (e.PropertyName == ExtendedEntry.IsBorderErrorVisibleProperty.PropertyName)
UpdateBorders();
}
void UpdateBorders()
{
GradientDrawable shape = new GradientDrawable();
shape.SetShape(ShapeType.Rectangle);
shape.SetCornerRadius(0);
if (((ExtendedEntry)this.Element).IsBorderErrorVisible)
{
shape.SetStroke(3, ((ExtendedEntry)this.Element).BorderErrorColor.ToAndroid());
}
else
{
shape.SetStroke(3, Android.Graphics.Color.LightGray);
this.Control.SetBackground(shape);
}
this.Control.SetBackground(shape);
}
}
}
Finally, Creating an Entry Behavior, handle the error to provide ui feedback to the user when validation occurs
public class EmptyEntryValidatorBehavior : Behavior<ExtendedEntry>
{
ExtendedEntry control;
string _placeHolder;
Xamarin.Forms.Color _placeHolderColor;
protected override void OnAttachedTo(ExtendedEntry bindable)
{
bindable.TextChanged += HandleTextChanged;
bindable.PropertyChanged += OnPropertyChanged;
control = bindable;
_placeHolder = bindable.Placeholder;
_placeHolderColor = bindable.PlaceholderColor;
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
ExtendedEntry customentry = (ExtendedEntry)sender;
if (!string.IsNullOrEmpty(customentry.Text))
{
((ExtendedEntry)sender).IsBorderErrorVisible = false;
}
else
{
((ExtendedEntry)sender).IsBorderErrorVisible = true;
}
}
protected override void OnDetachingFrom(ExtendedEntry bindable)
{
bindable.TextChanged -= HandleTextChanged;
bindable.PropertyChanged -= OnPropertyChanged;
}
void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == ExtendedEntry.IsBorderErrorVisibleProperty.PropertyName && control != null)
{
if (control.IsBorderErrorVisible)
{
control.Placeholder = control.ErrorText;
control.PlaceholderColor = control.BorderErrorColor;
control.Text = string.Empty;
}
else
{
control.Placeholder = _placeHolder;
control.PlaceholderColor = _placeHolderColor;
}
}
}
}
Update:
You can change custom entry's IsBorderErrorVisible in button.click, to call this from submit button.
private void btn1_Clicked(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(entry1.Text))
{
entry1.IsBorderErrorVisible = true;
}
}
<customentry:ExtendedEntry
x:Name="entry1"
BorderErrorColor="Red"
ErrorText="please enter name!">
<customentry:ExtendedEntry.Behaviors>
<behaviors:EmptyEntryValidatorBehavior />
</customentry:ExtendedEntry.Behaviors>
</customentry:ExtendedEntry>
I have a HybridWebView in my app and i need to change the uri dynamically (When the user click at some menu the uri changes), but it does not change at all... I use WebView and it works great, but when I use the HybridWebView the Uri is not changing.
This ContentView where is my HybridWebView is inside an AbsoluteLayout...
<ContentView AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1"
x:Name="contentView">
<local1:HybridWebView x:Name="hybridWebView"/></ContentView>
This is the code behind (Where i handle the click on the menus to change the url, every click is a different content):
public async void ChangeURL() { hybridWebView.Uri = MyUrl; }
This is my HybridWebView class:
public class HybridWebView : WebView
{
Action<string> action;
public static readonly BindableProperty UriProperty = BindableProperty.Create(
propertyName: nameof(Uri),
returnType: typeof(string),
declaringType: typeof(HybridWebView),
defaultValue: default(string));
public string Uri
{
get { return (string)GetValue(UriProperty); }
set { SetValue(UriProperty, value); }
}
public void RegisterAction(Action<string> callback)
{
action = callback;
}
public void Cleanup()
{
action = null;
}
public void InvokeAction(string data)
{
if (action == null || data == null)
{
return;
}
action.Invoke(data);
}
public HybridWebView()
{
}
}
I just resolved it like this:
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Control.Url != null)
{
var idn = new System.Globalization.IdnMapping();
var dotnetUri = new System.Uri(Element.Uri);
_url = ((HybridWebView)Element).Uri;
NSUrl nsUrl = new NSUrl(dotnetUri.Scheme, idn.GetAscii(dotnetUri.DnsSafeHost), dotnetUri.PathAndQuery);
Control.LoadRequest(new NSUrlRequest(nsUrl));
}
}
I had to add the OnElementPropertyChanged to change the url dynamically by the CustomRenderer
I am tying to further understand MVVM with some example scenario. I have a rootpage with a 'maindisplay' textblock. I would like to display 'status' or 'scenarios' from activation of any form of UI eg. togglebutton on the 'maindisplay' textblock.
I am able to bind the the page navigation info in the rootpageviewmodel to the textblock. However, I am not able to achieve the result when displaying info from different page.
I have checked another post multiple-viewmodels-in-same-view & Accessing a property in one ViewModel from another it's quite similar but it didn't work.
Please help. Thanks.
While accessing the RootPageViewModel should retain the instance?
View
<TextBlock Text="{x:Bind RootViewModel.MainStatusContent, Mode=OneWay}"/>
RootPage.xaml.cs
public sealed partial class RootPage : Page
{
private static RootPage instance;
public RootPageViewModel RootViewModel { get; set; }
public RootPage()
{
RootViewModel = new RootPageViewModel();
this.InitializeComponent();
// Always use the cached page
this.NavigationCacheMode = NavigationCacheMode.Required;
}
public static RootPage Instance
{
get
{
if (instance == null)
{
instance = new RootPage();
}
return instance;
}
}
private void nvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
contentFrame.Navigate(typeof(SettingsPage));
RootViewModel.MainStatusContent = "Settings_Page";
}
else
{
var navItemTag = args.InvokedItemContainer.Tag.ToString();
RootViewModel.MainStatusContent = navItemTag;
switch (navItemTag)
{
case "Home_Page":
contentFrame.Navigate(typeof(HomePage));
break;
case "Message_Page":
contentFrame.Navigate(typeof(MessagePage));
break;
}
}
}
}
RootPage ViewModel:
public class RootPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private static RootPageViewModel instance = new RootPageViewModel();
public static RootPageViewModel Instance
{
get
{
if (instance == null)
instance = new RootPageViewModel();
return instance;
}
}
public RootPageViewModel()
{
}
private string _mainStatusContent;
public string MainStatusContent
{
get
{
return _mainStatusContent;
}
set
{
_mainStatusContent = value;
OnPropertyChanged();
}
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
MessagePage.xaml.cs - to access RootPage ViewModel
public sealed partial class MessagePage : Page
{
public MessagePageViewModel MessageViewModel { get; set; }
public MessagePage()
{
MessageViewModel = new MessagePageViewModel();
this.InitializeComponent();
// Always use the cached page
this.NavigationCacheMode = NavigationCacheMode.Required;
}
private void Message1_Checked(object sender, RoutedEventArgs e)
{
RootPageViewModel.Instance.MainStatusContent = "Message 1 Selected";
}
private void Message1_Unchecked(object sender, RoutedEventArgs e)
{
RootPageViewModel.Instance.MainStatusContent = "Message 1 De-Selected";
}
}
When I debug the value did write to the instance but did't update the TextBlock. Did I do anything wrong in my XAML binding?
UWP C# MVVM How To Access ViewModel from Other Page
The better way is make static variable for RootPage, but not make singleton instance for RootPage and RootPageViewModel.
For example:
public RootPage ()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
Instance = this;
RootViewModel = new RootPageViewModel();
}
public static RootPage Instance;
Usage
private void Message1_Checked(object sender, RoutedEventArgs e)
{
RootPage.Instance.RootViewModel.MainStatusContent = "Message 1 Selected";
}
private void Message1_Unchecked(object sender, RoutedEventArgs e)
{
RootPage.Instance.RootViewModel.MainStatusContent = "Message 1 De-Selected";
}
I'm using Windows Template Studio (Prism) to create a test project, but from the documentation, I'm unable to figure out navigation. I now know how to do it with MVVM Light, but had to ask, because documentation doesn't detail it. How do I navigate from page to page in Prism Windows Template Studio?
Works for MVVM Light:
ViewModelLocator.Current.NavigationService.Navigate(typeof(MyViewModel).FullName, null)
Works on Template10:
BootStrapper.Current.NavigationService.Navigate(typeof(MyViewPage), null)
You could create test app with Windows Template Studio and check Navigation Page project type then check prism Design pattern. You will find the _navigationService in the ShellViewModel class.
Works on Windows Template Studio Prism
_navigationService.Navigate(pageKey, null);
ShellViewModel.cs
public class ShellViewModel : ViewModelBase
{
private static INavigationService _navigationService;
private WinUI.NavigationView _navigationView;
private bool _isBackEnabled;
private WinUI.NavigationViewItem _selected;
public ICommand ItemInvokedCommand { get; }
public bool IsBackEnabled
{
get { return _isBackEnabled; }
set { SetProperty(ref _isBackEnabled, value); }
}
public WinUI.NavigationViewItem Selected
{
get { return _selected; }
set { SetProperty(ref _selected, value); }
}
public ShellViewModel(INavigationService navigationServiceInstance)
{
_navigationService = navigationServiceInstance;
ItemInvokedCommand = new DelegateCommand<WinUI.NavigationViewItemInvokedEventArgs>(OnItemInvoked);
}
public void Initialize(Frame frame, WinUI.NavigationView navigationView)
{
_navigationView = navigationView;
frame.NavigationFailed += (sender, e) =>
{
throw e.Exception;
};
frame.Navigated += Frame_Navigated;
_navigationView.BackRequested += OnBackRequested;
}
private void OnItemInvoked(WinUI.NavigationViewItemInvokedEventArgs args)
{
var item = _navigationView.MenuItems
.OfType<WinUI.NavigationViewItem>()
.First(menuItem => (string)menuItem.Content == (string)args.InvokedItem);
var pageKey = item.GetValue(NavHelper.NavigateToProperty) as string;
_navigationService.Navigate(pageKey, null);
}
private void Frame_Navigated(object sender, NavigationEventArgs e)
{
IsBackEnabled = _navigationService.CanGoBack();
Selected = _navigationView.MenuItems
.OfType<WinUI.NavigationViewItem>()
.FirstOrDefault(menuItem => IsMenuItemForPageType(menuItem, e.SourcePageType));
}
private void OnBackRequested(WinUI.NavigationView sender, WinUI.NavigationViewBackRequestedEventArgs args)
{
_navigationService.GoBack();
}
private bool IsMenuItemForPageType(WinUI.NavigationViewItem menuItem, Type sourcePageType)
{
var sourcePageKey = sourcePageType.Name;
sourcePageKey = sourcePageKey.Substring(0, sourcePageKey.Length - 4);
var pageKey = menuItem.GetValue(NavHelper.NavigateToProperty) as string;
return pageKey == sourcePageKey;
}
}
_navigationService comes from ShellViewModel construct method. And this instance was created in the App class.
protected override UIElement CreateShell(Frame rootFrame)
{
var shell = Container.Resolve<ShellPage>();
shell.SetRootFrame(rootFrame);
return shell;
}
I have trying to learn about EventHandler and I have to use a notification project.
Here is the link of project :
https://codeload.github.com/mike-eason/WPF_ToastNotifications/zip/master
All I did is changing .Net-framework from 4.5 to 4
And I faced with this error:
My class cannot be used as type parameter 'TEventArgs' in the generic
type or method 'System.EventHandler'
ToastNotification Class:
[TemplatePart(Name = "PART_DismissButton", Type = typeof(Button))]
public class ToastNotification : ContentControl
{
public event EventHandler Dismissed;
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(ToastNotification));
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string), typeof(ToastNotification));
public ToastTypes ToastType
{
get { return (ToastTypes)GetValue(ToastTypeProperty); }
set { SetValue(ToastTypeProperty, value); }
}
public static readonly DependencyProperty ToastTypeProperty =
DependencyProperty.Register("ToastType", typeof(ToastTypes), typeof(ToastNotification), new PropertyMetadata(new PropertyChangedCallback(OnToastTypeChanged)));
private static void OnToastTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ToastNotification toast = (ToastNotification)d;
toast.RefreshBackgroundColour();
}
private void RefreshBackgroundColour()
{
switch (ToastType)
{
case ToastTypes.Success:
Background = ColourSuccess;
break;
case ToastTypes.Error:
Background = ColourDanger;
break;
case ToastTypes.Info:
Background = ColourInfo;
break;
case ToastTypes.Warning:
Background = ColourWarning;
break;
}
}
public bool IsPersistent
{
get { return (bool)GetValue(IsPersistentProperty); }
set { SetValue(IsPersistentProperty, value); }
}
public static readonly DependencyProperty IsPersistentProperty =
DependencyProperty.Register("IsPersistent", typeof(bool), typeof(ToastNotification));
public double FontSizeTitle
{
get { return (double)GetValue(FontSizeTitleProperty); }
set { SetValue(FontSizeTitleProperty, value); }
}
public static readonly DependencyProperty FontSizeTitleProperty =
DependencyProperty.Register("FontSizeTitle", typeof(double), typeof(ToastNotification));
public Brush ColourSuccess
{
get { return (Brush)GetValue(ColourSuccessProperty); }
set { SetValue(ColourSuccessProperty, value); }
}
public static readonly DependencyProperty ColourSuccessProperty =
DependencyProperty.Register("ColourSuccess", typeof(Brush), typeof(ToastNotification));
public Brush ColourDanger
{
get { return (Brush)GetValue(ColourDangerProperty); }
set { SetValue(ColourDangerProperty, value); }
}
public static readonly DependencyProperty ColourDangerProperty =
DependencyProperty.Register("ColourDanger", typeof(Brush), typeof(ToastNotification));
public Brush ColourInfo
{
get { return (Brush)GetValue(ColourInfoProperty); }
set { SetValue(ColourInfoProperty, value); }
}
public static readonly DependencyProperty ColourInfoProperty =
DependencyProperty.Register("ColourInfo", typeof(Brush), typeof(ToastNotification));
public Brush ColourWarning
{
get { return (Brush)GetValue(ColourWarningProperty); }
set { SetValue(ColourWarningProperty, value); }
}
public static readonly DependencyProperty ColourWarningProperty =
DependencyProperty.Register("ColourWarning", typeof(Brush), typeof(ToastNotification));
static ToastNotification()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToastNotification), new FrameworkPropertyMetadata(typeof(ToastNotification)));
}
public ToastNotification()
{
this.Loaded += ToastNotification_Loaded;
}
private void ToastNotification_Loaded(object sender, RoutedEventArgs e)
{
Storyboard sb = this.FindResource("ToastScaleInStoryboard") as Storyboard;
Storyboard.SetTarget(sb, this);
sb.Begin();
}
public override void OnApplyTemplate()
{
ButtonBase PART_DismissButton = this.GetTemplateChild("PART_DismissButton") as ButtonBase;
if (PART_DismissButton != null)
PART_DismissButton.Click += OnDismissed;
base.OnApplyTemplate();
RefreshBackgroundColour();
}
protected void OnDismissed(object sender, RoutedEventArgs e)
{
var eh = Dismissed;
if (eh != null)
eh(this, EventArgs.Empty);
}
}
Toast Class:
internal class Toast
{
public event EventHandler<ToastNotification> ToastClosing;
private DispatcherTimer _Timer;
private ToastNotification _Notification;
public Toast(ToastNotification notification)
{
_Notification = notification;
_Notification.Dismissed += Notification_Dismissed;
}
private void Notification_Dismissed(object sender, EventArgs e)
{
OnToastClosing();
}
private void Timer_Tick(object sender, EventArgs e)
{
//Stop and close the window.
_Timer.Stop();
OnToastClosing();
}
public void Show(TimeSpan displayTime)
{
//Only start the timer if the notification is not persistent.
if (!_Notification.IsPersistent)
{
//Set up the timer
_Timer = new DispatcherTimer();
_Timer.Interval = displayTime;
_Timer.Tick += Timer_Tick;
//Start the timer
_Timer.Start();
}
}
protected void OnToastClosing()
{
//Unsubscribe from the on dismiss event first (to avoid memory leaks)
_Notification.Dismissed -= Notification_Dismissed;
var eh = ToastClosing;
if (eh != null)
eh(this, _Notification);
}
}
System.EventHandler<T> delegate was changed in .NET 4.5. Before 4.5 it has the following signature:
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e)
where TEventArgs : EventArgs;
Starting with .NET 4.5 it has another definition:
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
Note that where ... part has been removed. So before .NET 4.5 the type you use for event handler argument should inherit from EventArgs. Your ToastNotification does not inherit from that class, so cannot be used, hence your compiler error. When project targets .NET 4.5+ - you can use any type there so it compiles fine.
You can change your ToastClosing to
public event Action<object, ToastNotification> ToastClosing;
and it will compile just fine.
public event Action<object, ToastNotification> ToastClosing;
I think the recommendation is to inherit 'EventArgs'.
public class MyEventArgs : EventArgs
{ }
public event EventHandler<MyEventArgs> MyEvent;