WPF 找不到App.xaml里面的样式的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 找不到App.xaml里面的样式的问题相关的知识,希望对你有一定的参考价值。
我把样式写在App.xaml里面,没运行时候能看到效果,运行时候要报错:“在“System.Windows.StaticResourceExtension”上提供值时引发了异常。怎么解决啊
参考技术A 重新编译再试看下!一般没运行时能看到,运行的时候也应当看到的。你样式的key有没有设置正确呢?如果把app里的样式剪切到page的resource里运行还会报错么?本回答被提问者采纳 参考技术B 样式定义和使用的代码段都贴出来看看才知道。WPF动态创建消息弹窗,2秒自动消失
样式代码,放入App.xaml中
App.xaml
<Application x:Class="WpfSnqkGasAnalysis.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfSnqkGasAnalysis"
StartupUri="Login.xaml">
<Application.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>
</Application>
Dictionary1.xaml,消息框样式
<!--消息提示框-->
<Style x:Key="TextBlock_Alert" TargetType="TextBlock">
<Setter Property="Background" Value="Lime"></Setter>
<Setter Property="Padding" Value="20,10"></Setter>
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Margin" Value="4"></Setter>
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="VerticalAlignment" Value="Top"></Setter>
<Setter Property="TextWrapping" Value="Wrap"></Setter>
<!--<Setter Property="Panel.ZIndex" Value="3000"></Setter>-->
</Style>
后端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace WpfSnqkGasAnalysis
/// <summary>
/// 消息框封装,消息2秒后消失
/// </summary>
/// 创建时间:2022年11月30日15:08:25。
public static class ShowAlertHelpter
//上一次显示的top位置
static double lastTop = 0;
static double lastLeft = 0;
//上一次显示时间
static DateTime lastShowTime = DateTime.Now;
/// <summary>
/// 提示消息。消息2秒后消失
/// </summary>
/// <param name="msg">消息</param>
/// <param name="alwaysShow">是否一直显示</param>
public static void ShowAlert(string msg, bool alwaysShow = false)
//Grid grid = Application.Current.Windows[0].FindName("mainGrid") as Grid;
Grid grid = Application.Current.Windows[0].Content as Grid;
if (grid == null)
return;
//Grid grid = null;
//if (page is Window)
//
// Window window = (Window)page;
// grid = window.Content as Grid;
//
//else if (page is Page)
//
// Page args = (Page)page;
// grid = args.Content as Grid;
//
//else if (page is Grid)
//
// grid = page as Grid;
//
Application.Current.Dispatcher.BeginInvoke(
() =>
//Popup使消息在最前面显示
Popup popup = new Popup();
popup.PopupAnimation = PopupAnimation.Fade;//渐入
popup.AllowsTransparency = true;//设置Popup透明
popup.Margin = new Thickness(10);
popup.VerticalOffset = 200;
popup.HorizontalOffset = 600;
popup.HorizontalAlignment = HorizontalAlignment.Center;
//popup.IsOpen = false;
popup.IsOpen = true;
popup.ToolTip = "点击关闭";
TextBlock textBlock = new TextBlock();
textBlock.Text = msg;
textBlock.Style = (System.Windows.Style)grid.FindResource("TextBlock_Alert");
//边框阴影
System.Windows.Media.Effects.DropShadowEffect effect = new System.Windows.Media.Effects.DropShadowEffect();
effect.BlurRadius = 4;
effect.Color = Color.FromRgb(196, 196, 196);
textBlock.Effect = effect;
if (lastTop > 0 && (DateTime.Now - lastShowTime).TotalSeconds < 3)
//字符多,增加偏移
double space = 50d;
if (msg.Length > 22)
space = 70d;
popup.HorizontalOffset = lastLeft + space;
popup.VerticalOffset = lastTop + space;
//一直显示消息
if (alwaysShow || msg.Contains("失败") || msg.Contains("异常") || msg.Contains("出错"))
textBlock.Background = Brushes.Orchid;//红色背景
goto SKIP_END;
//消息提示自动消失
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(2000);
timer.Tick += (s, e) =>
grid.Children.Remove(popup);
timer.Stop();
if (lastTop > 500)
lastTop = 0;
lastLeft = -200;
;
timer.Start();
SKIP_END:
lastTop = popup.VerticalOffset;
lastLeft = popup.HorizontalOffset;
lastShowTime = DateTime.Now;
popup.Child = textBlock;
grid.Children.Add(popup);
//点击就移除
popup.MouseLeftButtonUp += (s, e) =>
grid.Children.Remove(popup);
;
);
调用显示消息
private void btnSearch_Click(object sender, RoutedEventArgs e)
ShowAlertHelpter.ShowAlert("操作成功");
以上是关于WPF 找不到App.xaml里面的样式的问题的主要内容,如果未能解决你的问题,请参考以下文章