当前位置:网站首页>Using lazy < t > in C # to realize singleton mode in WPF
Using lazy < t > in C # to realize singleton mode in WPF
2022-06-30 05:37:00 【Rodinia lava】
Lazy Is a deferred object , It allows you to delay the creation of T object , in other words , Although the object has been declared , But actually only in use T When the object , To create this object .
stay WPF in , Public methods Can be written in a separate class , each viewmodel Use it when you need it Lazy call .
for example
Create public classes appData
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
namespace ShenhuaSQLite
{
public class AppData: ObservableObject
{
public AppData()
{
}
public static AppData Instance = new Lazy<AppData>(() => new AppData()).Value;
private string systemName = "Zg Upper computer test ";
public string SystemName
{
get {
return systemName; }
set {
systemName = value; }
}
private SysAdmins sysAdmins = new SysAdmins();
public SysAdmins CurrentUser
{
get {
return sysAdmins; }
set {
sysAdmins = value; RaisePropertyChanged(); }
}
/// <summary>
/// Main form
/// </summary>
public MainWindow MainWindow {
get; set; } = null;
/// <summary>
/// Show or hide mask layers
/// </summary>
/// <param name="isMark"></param>
public void ShowMarkLayer(bool isMark)
{
if (MainWindow == null) return;
//MainWindow.markLayer.Visibility = isMark ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
}
}
}
In the login form , You can instantiate in the constructor lazy, Implementation calls appData All properties and methods in . This is somewhat similar to the multiple background data blocks on the way to the blog .
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DAL;
namespace ShenhuaSQLite.ViewModel
{
public class LoginViewModel : ViewModelBase
{
public AppData appData {
get; private set; } = AppData.Instance;
public LoginViewModel()
{
this.appData.CurrentUser.LoginName = "ZG";
this.appData.CurrentUser.LoginPwd = "123";
}
public RelayCommand<Window> LoginCommand2 // Just write get Return
{
get
{
return new RelayCommand<Window>((arg) =>
{
SysAdminsProvider sysAdminsProvider = new SysAdminsProvider();
//MemberProvider memberProvider = new MemberProvider();
var list = sysAdminsProvider.Select();
var user = list.FirstOrDefault(
u => u.LoginName == appData.CurrentUser.LoginName
&& u.LoginPwd == appData.CurrentUser.LoginPwd) ; //u For database objects
if (user == null)
{
MessageBox.Show(" Wrong user name or password !");
}
else
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
arg.Close();
}
});
}
}
}
}

边栏推荐
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Unity C trigonometric function, right triangle corner calculation
- PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
- Responsive flow layout
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- After reading who moved my cheese
- Visualization of 3D geological model based on borehole data by map flapping software
- Who is promoting the new inflection point of audio and video industry in 2022?
- Access is denied encountered when vfpbs calls excel under IIS
- What are membrane stress and membrane strain
猜你喜欢

Idea of capturing mobile terminal variant combination

企事业单位源代码防泄露工作该如何进行

聲網,站在物聯網的“土壤”裏

强烈推荐十几款IDEA开发必备的插件

终端便捷ssh(免密)连接

抓取手机端变体组合思路设想

Summary of common loss functions in pytorch

如何制作CSR(Certificate Signing Request)文件?

The minecraft server address cannot be refreshed.

Use the code cloud publicholiday project to determine whether a day is a working day
随机推荐
3D rotation album
Unityshader learning notes - Basic Attributes
Another download address for typro
Baiwen.com 7 days Internet of things smart home learning experience punch in the third day
hashlips_ art_ Engine-1.0.6 usage
[notes] unity Scrollview button page turning
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (II)
Promise知识点拾遗
Unity- the camera follows the player
强烈推荐十几款IDEA开发必备的插件
Expansion method of unity scanning circle
网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
使用码云PublicHoliday项目判断某天是否为工作日
We strongly recommend more than a dozen necessary plug-ins for idea development
C. Divan and bitwise operations
The minecraft server address cannot be refreshed.
Assembly learning tutorial: accessing memory (3)
Rotation, translation and scaling of unity VR objects
Database SQL language 06 single line function
Unity Catmull ROM curve