上海專業(yè)網站建設價格抖音seo軟件工具
? ? ? ? 其實,在wpf中,最核心的就是xaml,因為只有xaml,才能體現(xiàn)出用的是wpf,而不是普通的cs文件,cs文件在winform中等等程序都可以使用的,唯獨xaml才是wpf中最重要的,最精華的東西,但是xaml說深也深,說淺也淺,很多人都是用winform的做法去開發(fā)wpf,從效果上看,沒有任何區(qū)別的。
? ? ? ? 今天說一下wpf中的資源,其實也屬于xaml中的內容,萬物皆資源。在資源中,我們可以插入UC控件以及ViewModel。
1.首先創(chuàng)建一個wpf程序
2. 把UC控件當做資源來使用
2.1首先創(chuàng)建一個UC界面
2.2在App.xaml中把它當做資源
<Application x:Class="WpfApp2.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp2"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><local:UserControl1 x:Key="ucTest"/><ResourceDictionary.MergedDictionaries></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>
2.3在主界面直接調用
<Window x:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><ContentControl Content="{StaticResource ucTest}" /></Grid>
</Window>
2.4 效果
此時其實就是把UC控件充當了引用界面的方式,效果一毛一樣。?
3.把ViewModel當做資源來使用
3.1接著上面的代碼繼續(xù),我們采用簡單的MVVM模式
建立MainViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace WpfApp2
{public class MainViewModel : BindingBase{public MainViewModel(){}private string name = "故里2130";public string Name{get { return name; }set{name = value; OnPropertyChanged();//OnPropertyChanged(nameof(name),使用特性,去掉括號的值}}}public class BindingBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;//protected virtual void OnPropertyChanged(string propertyName)protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")//此處使用特性{PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
}
3.2在App.xaml中把它當做資源
<Application x:Class="WpfApp2.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp2"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><local:UserControl1 x:Key="ucTest"/><local:MainViewModel x:Key="vmTest"/><ResourceDictionary.MergedDictionaries></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>
3.3然后在界面中調用
<Window x:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"DataContext="{StaticResource vmTest}"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBlock Text="{Binding Name}"/><ContentControl Content="{StaticResource ucTest}" /></StackPanel>
</Window>
3.4效果
?
然后可以直接綁定屬性的值,非常的方便,不得不說,這個功能很nice。
源碼:
https://download.csdn.net/download/u012563853/88623422
來源:
巧妙的使用WPF中的資源-CSDN博客