Я пытаюсь сделать границу в качестве уведомления, работая с WPF.Итак, я создал UserControle, представляющий карту уведомлений, вот код Xaml:
<UserControl x:Class="Try2.OurNotification"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Try2="clr-namespace:Try2"
Width="350">
<Border Name="BrdNotifay" Style="{StaticResource {ComponentResourceKey
TypeInTargetAssembly={x:Type Try2:ThemeKey}, ResourceId=ToastCard}}">
<DockPanel Margin="5">
<TextBlock DockPanel.Dock="Left" Style="{StaticResource
{ComponentResourceKey TypeInTargetAssembly={x:Type Try2:ThemeKey},
ResourceId=ToastIcon}}">
<Canvas Style="{StaticResource {ComponentResourceKey
TypeInTargetAssembly={x:Type Try2:ThemeKey}, ResourceId=ToastIconCanvas}}">
<Path x:Name="Ico" Width="38" Height="38"
Canvas.Left="19" Canvas.Top="19" Stretch="Fill" Fill="White" Data="F1 M
31.6667,19L 44.3333,19L 57,31.6667L 57,44.3333L 44.3333,57L 31.6667,57L
19,44.3333L 19,31.6667L 31.6667,19 Z M 26.4762,45.0454L 30.9546,49.5238L
38,42.4783L 45.0454,49.5238L 49.5237,45.0454L 42.4783,38L 49.5238,30.9546L
45.0454,26.4763L 38,33.5217L 30.9546,26.4762L 26.4762,30.9546L 33.5217,38L
26.4762,45.0454 Z "/>
</Canvas>
</TextBlock>
<StackPanel DockPanel.Dock="Right">
<TextBlock Text="{Binding Title, RelativeSource={RelativeSource
AncestorType=Try2:OurNotification, Mode=FindAncestor}}"
Style="{StaticResource {ComponentResourceKey
TypeInTargetAssembly={x:Type Try2:ThemeKey}, ResourceId=ToastTitle}}" />
<TextBlock Text="{Binding Message, RelativeSource=
{RelativeSource AncestorType=Try2:OurNotification, Mode=FindAncestor}}"
Style="{StaticResource {ComponentResourceKey
TypeInTargetAssembly={x:Type Try2:ThemeKey}, ResourceId=ToastMessage}}" />
</StackPanel>
</DockPanel>
</Border>
</UserControl>
, а вот код этого "UserControl"
public OurNotification()
{
InitializeComponent();
}
public OurNotification(FrameworkElement owner, string title, string message,
ToasterAnimation animation, double margin)
{
InitializeComponent();
Title = title;
Message = message;
Animation = animation;
Margins = margin;
var story = NotifiayDefinations.GetAnimation(Animation,BrdNotifay);
story.Completed += (sender, args) => { Visibility = Visibility.Hidden; };
story.Begin(BrdNotifay);
}
#region Message
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
// Using a DependencyProperty as the backing store for Message.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register(
"Message", typeof(string), typeof(OurNotification), new UIPropertyMetadata(string.Empty));
#endregion
#region Title
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Message.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register(
"Title", typeof(string), typeof(OurNotification), new UIPropertyMetadata(string.Empty));
#endregion
#region Animation
public ToasterAnimation Animation
{
get { return (ToasterAnimation)GetValue(AnimationProperty); }
set { SetValue(AnimationProperty, value); }
}
// Using a DependencyProperty as the backing store for Message.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty AnimationProperty =
DependencyProperty.Register(
"Animation", typeof(ToasterAnimation), typeof(OurNotification), new UIPropertyMetadata(ToasterAnimation.SlideInFromTop));
#endregion
#region Margins
public double Margins
{
get { return (double)GetValue(MarginsProperty); }
set { SetValue(MarginsProperty, value); }
}
// Using a DependencyProperty as the backing store for Message.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty MarginsProperty =
DependencyProperty.Register(
"Margins", typeof(double), typeof(OurNotification), new UIPropertyMetadata(0D));
#endregion
У меня есть словарь и класс темы"themekey" для изменения тем, однако, я создал класс "NotifiayDefination.cs", который содержит все необходимые определения и метод анимации.вот код.
public static class Helper
{
public static SolidColorBrush ToSolidColorBrush(this string hex)
{
var convertFromString = ColorConverter.ConvertFromString(hex);
var color = (Color?)convertFromString ?? Colors.Black;
return new SolidColorBrush(color);
}
}
public static class NotificationIcoPath
{
public static readonly Geometry Success = Geometry.Parse("F1 M23.7501 37.25 L34.8334 48.3333 L52.2499 26.1668 L56.9999 30.9168 L34.8334 57.8333 L19.0001 42 L23.7501 37.25 Z ");
public static readonly Geometry Info = Geometry.Parse("F1 M 38,19C 48.4934,19 57,27.5066 57,38C 57,48.4934 48.4934,57 38,57C 27.5066,57 19,48.4934 19,38C 19,27.5066 27.5066,19 38,19 Z M 33.25,33.25L 33.25,36.4167L 36.4166,36.4167L 36.4166,47.5L 33.25,47.5L 33.25,50.6667L 44.3333,50.6667L 44.3333,47.5L 41.1666,47.5L 41.1666,36.4167L 41.1666,33.25L 33.25,33.25 Z M 38.7917,25.3333C 37.48,25.3333 36.4167,26.3967 36.4167,27.7083C 36.4167,29.02 37.48,30.0833 38.7917,30.0833C 40.1033,30.0833 41.1667,29.02 41.1667,27.7083C 41.1667,26.3967 40.1033,25.3333 38.7917,25.3333 Z ");
public static readonly Geometry Error = Geometry.Parse("F1 M 31.6667,19L 44.3333,19L 57,31.6667L 57,44.3333L 44.3333,57L 31.6667,57L 19,44.3333L 19,31.6667L 31.6667,19 Z M 26.4762,45.0454L 30.9546,49.5238L 38,42.4783L 45.0454,49.5238L 49.5237,45.0454L 42.4783,38L 49.5238,30.9546L 45.0454,26.4763L 38,33.5217L 30.9546,26.4762L 26.4762,30.9546L 33.5217,38L 26.4762,45.0454 Z ");
public static readonly Geometry Warning = Geometry.Parse("F1 M 38,19C 48.4934,19 57,27.5066 57,38C 57,48.4934 48.4934,57 38,57C 27.5066,57 19,48.4934 19,38C 19,27.5066 27.5066,19 38,19 Z M 34.0417,25.7292L 36.0208,41.9584L 39.9792,41.9583L 41.9583,25.7292L 34.0417,25.7292 Z M 38,44.3333C 36.2511,44.3333 34.8333,45.7511 34.8333,47.5C 34.8333,49.2489 36.2511,50.6667 38,50.6667C 39.7489,50.6667 41.1667,49.2489 41.1667,47.5C 41.1667,45.7511 39.7489,44.3333 38,44.3333 Z ");
}
public static class NotificationColor
{
public static readonly SolidColorBrush Success = "#4BA253".ToSolidColorBrush();
public static readonly SolidColorBrush Info = "#5EAEC5".ToSolidColorBrush();
public static readonly SolidColorBrush Error = "#C43829".ToSolidColorBrush();
public static readonly SolidColorBrush Warning = "#FF9400".ToSolidColorBrush();
}
public enum ToastType
{
Error,
Info,
Success,
Warning
}
public enum ToasterAnimation
{
FadeIn,
SlideInFromRight,
SlideInFromLeft,
SlideInFromTop,
SlideInFromBottom,
GrowFromRight,
GrowFromLeft,
GrowFromTop,
GrowFromBottom,
}
internal class NotifiayDefinations
{
public static StoryBoard GetAnimation(ToasterAnimation animation, UIElement toaster)
{
var story = new StoryBoard();
switch (animation)
{
case ToasterAnimation.FadeIn:
DoubleAnimation fadein = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(2))
{
BeginTime = TimeSpan.FromSeconds(0)
};
Storyboard.SetTargetProperty(fadein, new PropertyPath("(UIElement.Opacity)"));
story.Children.Add(fadein);
DoubleAnimation fadeout = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(2))
{
BeginTime = TimeSpan.FromSeconds(4)
};
Storyboard.SetTargetProperty(fadeout, new PropertyPath("(UIElement.Opacity)"));
story.Children.Add(fadeout);
break;
case ToasterAnimation.SlideInFromRight:
toaster.RenderTransformOrigin = new Point(1, 0);
toaster.RenderTransform = new TranslateTransform(300.0, 0);
var slideinFromRightAnimation = new DoubleAnimationUsingKeyFrames
{
Duration = new Duration(TimeSpan.FromSeconds(6)),
KeyFrames = new DoubleKeyFrameCollection
{
new EasingDoubleKeyFrame(300.0, KeyTime.FromPercent(0)),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.1), new ExponentialEase
{
EasingMode = EasingMode.EaseInOut
}),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.8)),
new EasingDoubleKeyFrame(300.0, KeyTime.FromPercent(0.9), new ExponentialEase
{
EasingMode = EasingMode.EaseOut
})
}
};
Storyboard.SetTargetProperty(slideinFromRightAnimation,
new PropertyPath("RenderTransform.(TranslateTransform.X)"));
story.Children.Add(slideinFromRightAnimation);
break;
case ToasterAnimation.SlideInFromLeft:
toaster.RenderTransformOrigin = new Point(0, 1);
toaster.RenderTransform = new TranslateTransform(-300.0, 0);
var slideinFromLeftAnimation = new DoubleAnimationUsingKeyFrames
{
Duration = new Duration(TimeSpan.FromSeconds(6)),
KeyFrames = new DoubleKeyFrameCollection
{
new EasingDoubleKeyFrame(-300.0, KeyTime.FromPercent(0)),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.1), new ExponentialEase
{
EasingMode = EasingMode.EaseInOut
}),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.8)),
new EasingDoubleKeyFrame(-300.0, KeyTime.FromPercent(0.9), new ExponentialEase
{
EasingMode = EasingMode.EaseOut
})
}
};
Storyboard.SetTargetProperty(slideinFromLeftAnimation,
new PropertyPath("RenderTransform.(TranslateTransform.X)"));
story.Children.Add(slideinFromLeftAnimation);
break;
case ToasterAnimation.SlideInFromTop:
toaster.RenderTransformOrigin = new Point(0, 1);
toaster.RenderTransform = new TranslateTransform(0, 300);
var slideinFromTopAnimation = new DoubleAnimationUsingKeyFrames
{
Duration = new Duration(TimeSpan.FromSeconds(6)),
KeyFrames = new DoubleKeyFrameCollection
{
new EasingDoubleKeyFrame(-300.0, KeyTime.FromPercent(0)),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.1), new ExponentialEase
{
EasingMode = EasingMode.EaseInOut
}),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.8)),
new EasingDoubleKeyFrame(-300.0, KeyTime.FromPercent(0.9), new ExponentialEase
{
EasingMode = EasingMode.EaseOut
})
}
};
Storyboard.SetTargetProperty(slideinFromTopAnimation,
new PropertyPath("RenderTransform.(TranslateTransform.Y)"));
story.Children.Add(slideinFromTopAnimation);
break;
case ToasterAnimation.SlideInFromBottom:
toaster.RenderTransformOrigin = new Point(0, 1);
toaster.RenderTransform = new TranslateTransform(0, -300);
var slideinFromBottomAnimation = new DoubleAnimationUsingKeyFrames
{
Duration = new Duration(TimeSpan.FromSeconds(6)),
KeyFrames = new DoubleKeyFrameCollection
{
new EasingDoubleKeyFrame(300.0, KeyTime.FromPercent(0)),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.1), new ExponentialEase
{
EasingMode = EasingMode.EaseInOut
}),
new EasingDoubleKeyFrame(0.0, KeyTime.FromPercent(0.8)),
new EasingDoubleKeyFrame(300.0, KeyTime.FromPercent(0.9), new ExponentialEase
{
EasingMode = EasingMode.EaseOut
})
}
};
Storyboard.SetTargetProperty(slideinFromBottomAnimation,
new PropertyPath("RenderTransform.(TranslateTransform.Y)"));
story.Children.Add(slideinFromBottomAnimation);
break;
case ToasterAnimation.GrowFromRight:
toaster.RenderTransformOrigin = new Point(1, 0);
DoubleAnimationUsingKeyFrames growfromright = new DoubleAnimationUsingKeyFrames();
growfromright.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(0)));
growfromright.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(1.5)));
growfromright.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(4)));
growfromright.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(5.5)));
Storyboard.SetTargetProperty(growfromright,
new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
story.Children.Add(growfromright);
break;
case ToasterAnimation.GrowFromLeft:
toaster.RenderTransformOrigin = new Point(0, 1);
DoubleAnimationUsingKeyFrames growfromleft = new DoubleAnimationUsingKeyFrames();
growfromleft.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(0)));
growfromleft.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(1.5)));
growfromleft.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(4)));
growfromleft.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(5.5)));
Storyboard.SetTargetProperty(growfromleft,
new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
story.Children.Add(growfromleft);
break;
case ToasterAnimation.GrowFromTop:
toaster.RenderTransformOrigin = new Point(1, 0);
DoubleAnimationUsingKeyFrames growfromtop = new DoubleAnimationUsingKeyFrames();
growfromtop.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(0)));
growfromtop.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(1.5)));
growfromtop.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(4)));
growfromtop.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(5.5)));
Storyboard.SetTargetProperty(growfromtop,
new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));
story.Children.Add(growfromtop);
break;
case ToasterAnimation.GrowFromBottom:
toaster.RenderTransformOrigin = new Point(0, 1);
DoubleAnimationUsingKeyFrames growfrombottom = new DoubleAnimationUsingKeyFrames();
growfrombottom.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(0)));
growfrombottom.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(1.5)));
growfrombottom.KeyFrames.Add(new SplineDoubleKeyFrame(1, TimeSpan.FromSeconds(4)));
growfrombottom.KeyFrames.Add(new SplineDoubleKeyFrame(0, TimeSpan.FromSeconds(5.5)));
Storyboard.SetTargetProperty(growfrombottom,
new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));
story.Children.Add(growfrombottom);
break;
}
return story;
}
}
}
, чем я создал вызов с именем "CallNotifiactions.cs", чтобы изменить цвет и значок для карточки уведомлений, и я вызываю его, вот код "CallNotifiactions.cs "
public static class CallNotifiactions
{
private static OurNotification GetToasterWindow(FrameworkElement owner, ToastType type, string title, string message,
ToasterAnimation animation, double margin)
{
var toaster = new OurNotification(owner, title, message, animation, margin);
switch (type)
{
case ToastType.Error:
toaster.Ico.Data = NotificationIcoPath.Error;
toaster.BrdNotifay.Background = NotificationColor.Error;
break;
case ToastType.Info:
toaster.Ico.Data = NotificationIcoPath.Info;
toaster.BrdNotifay.Background = NotificationColor.Info;
break;
case ToastType.Warning:
toaster.Ico.Data = NotificationIcoPath.Warning;
toaster.BrdNotifay.Background = NotificationColor.Warning;
break;
case ToastType.Success:
toaster.Ico.Data = NotificationIcoPath.Success;
toaster.BrdNotifay.Background = NotificationColor.Success;
break;
}
return toaster;
}
public static void ShowError(
Window owner,
string title = "Error",
string message = "Something terrible may have just happened and you are being notified of it.",
ToasterAnimation animation = ToasterAnimation.SlideInFromRight,
double margin = 10.0)
{
var toaster = GetToasterWindow(owner, ToastType.Error, title, message, animation, margin);
toaster.Visibility=Visibility.Visible;
}
}
}
наконец я перехожу к" главному окну "и вызываю метод, отвечающий за то, какие уведомления появляются здесь:
CallNotifiactions.ShowError(this, "Error", "Boof", ToasterAnimation.SlideInFromTop, 10);
код работал нормально, но «UserControle» не виден, и я не понимаю, почему уведомление не отображается?Где ошибка؟
Надеюсь, пожалуйста, помогите мне,
Спасибо.