C# 为什么UserControle不可见?
我尝试将边界作为我与WPF合作的通知。因此,我创建了UserControle表示通知卡,下面是Xaml代码:C# 为什么UserControle不可见?,c#,wpf,notifications,C#,Wpf,Notifications,我尝试将边界作为我与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"
<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>
我有主题词典和“themekey”课程来改变主题,
我创建了类“NotifiayDefinitions.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;
}
}
}
然后我创建了名为“callNotifications.cs”的调用来更改通知卡的颜色和图标,我在这里称它为“callNotifications.cs”的代码
}
最后,我转到“mainwindow”,并调用负责这里出现的通知类型的方法
CallNotifiactions.ShowError(this, "Error", "Boof", ToasterAnimation.SlideInFromTop, 10);
代码运行良好,但“UserControle”不可见,我也不明白为什么通知不可见?
哪里出了错
我希望能帮助我
谢谢。请解释您试图做什么,以及在什么情况下您的“UserControle”不可见。另外,请只添加此问题所需的代码。我试图将“UserControle”显示为一个与此代码相关的通知,代码正常工作,但“UserControle”未显示在主窗口中
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);