if(NavigationService.CanGoBack == true) NavigationService.GoBack();
NavigationService.Navigate(new Uri("Page.xaml",UriKind.Relative));
public partial class App : Application
{protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);Mutex mutex = new Mutex(true, "MainWindow", out bool isNewInstance);if (!isNewInstance ){IntPtr intPtr = FindWindowW(null, "MainWindow");if (intPtr !=IntPtr.Zero){SetForegroundWindow(intPtr);}Shutdown();}}[DllImport("User32", CharSet = CharSet.Unicode)]static extern IntPtr FindWindowW(string ClassName, string WindowName);[DllImport("User32", CharSet = CharSet.Unicode)]static extern bool SetForegroundWindow(IntPtr hWnd);
}
string[] args = e.Args;//在程序级别捕获
protected override void OnStartup(StartupEventArgs e)
{base.OnStartup(e);DispatcherUnhandledException += App_DispatcherUnhandledException;
}private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{e.Handled = true;//异常已经处理
}
//在主窗口级别捕获,这样可以在程序级别重新启动主窗口
public MainWindow()
{InitializeComponent();AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{MessageBox.Show("dd");
}
//异常首先被程序级别捕获再传到窗口级别,如果在程序级别中不设置e.Handled = true;则会传到窗口级别,然后主程序挂掉。
//所以在主程序挂掉之前要重启
TextBlock合理Label相比,更加轻量化,Lable常用在可以连接到指定地方,加下划线。
img.Source = new ImageSourceConverter().ConvertFromString("path.png") as ImageSource;这种方式需要设置图片不能为资源
img.Source = new BitmapImage(new Uri("path.png", UriKind.RelativeOrAbsolute));
var cvs= CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);//获得默认视图if (cvs!=null && cvs.CanSort){cvs.SortDescriptions.Clear();cvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Descending));}
var cvs= CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);if (cvs!=null && cvs.CanGroup){cvs.GroupDescriptions.Clear();cvs.GroupDescriptions.Add(new PropertyGroupDescription("Group"));}
var cvs= CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);if (cvs!=null && cvs.CanFilter){cvs.Filter = (obj) =>{if (obj is Person per){return per.FirstName.Contans("TOM");}return false;};}
System.Windows.Interactivity.WPFpublic class GrowTextBehavior:Behavior
{public int Size { get; set; }protected override void OnAttached()//增加事件{base.OnAttached();AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;}protected override void OnDetaching()//减少事件{base.OnDetaching();AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;}//缩小private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){AssociatedObject.FontSize -= Size;}//放大private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e){AssociatedObject.FontSize += Size;}
}
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

Source="/LibraryName;component/Images/img.jpg"
在代码中访问Application.GetRemoteStream(new Uri("/MyLib;component/Img/R-C.png", UriKind.RelativeOrAbsolute));
在Application.Resources中合并其他资源字典
{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}
//下划线忽略不使用的变量
ThreadPool.QueueUserWorkItem(_ =>
{//其他现成更新UIDispatcher.BeginInvoke(new Action(()=> {this.btn.Content = "aa";}));
});
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;//支持取消
worker.DoWork += Worker_DoWork;//执行的任务事件
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;// 执行的任务完成事件
worker.RunWorkerAsync(new Tuple(1, 2));
worker.WorkerReportsProgress = true;//设置进度可用
worker.ProgressChanged += Worker_ProgressChanged;//计算进度更新
worker.CancelAsync();//取消
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(1.0);
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Start();
加入引用WindowsFormsIntegration和System.Windows.Forms
xaml中
proForms.SelectedObject = txtWPF;
直接拖控件
1)使用第三方库的WPF应用程序
2)修改项目文件
%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)
3)修改App.Xaml.cs
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args){Assembly executingAssembly = Assembly.GetExecutingAssembly();var executingAssemblyName = executingAssembly.GetName();var resName = executingAssemblyName.Name + ".resources";AssemblyName assemblyName = new AssemblyName(args.Name); string path = "";if (resName == assemblyName.Name){path = executingAssemblyName.Name + ".g.resources"; ;}else{path = assemblyName.Name + ".dll";if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false){path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);}}using (Stream stream = executingAssembly.GetManifestResourceStream(path)){if (stream == null)return null;byte[] assemblyRawBytes = new byte[stream.Length];stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);return Assembly.Load(assemblyRawBytes);}}protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;}
C#调用win32API参考网站
http://www.pinvoke.net