当前位置:网站首页>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();
}
});
}
}
}
}

边栏推荐
- E: Topic focus
- AI大模型落地大考,浪潮交出了怎样的答卷?
- PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
- Unity limited time use limited trial time and use times
- unity 扫描圈 圆扩展方法
- Video summary of my station B
- Unity- the camera follows the player
- Xiaosha's lunch
- Promise知识点拾遗
- Unity publishing /build settings
猜你喜欢

How to judge the quality of network transformer? What symptom is network filter transformer broken?

Did you know that WPS can turn on eye protection mode?

mmcv常用API介绍

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

Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself

14x1.5cm竖向标签有点难,VFP调用BarTender来打印

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

Sword finger offer 22 The penultimate node in the linked list

Unity- the camera follows the player

Xctf--Web--Challenge--area Wp
随机推荐
Access is denied encountered when vfpbs calls excel under IIS
终端便捷ssh(免密)连接
Database SQL language 06 single line function
14x1.5cm vertical label is a little difficult, VFP calls bartender to print
[typescript] cannot redeclare block range variables
C # uses monopinvokecallback to directly call back C # function
hashlips_ art_ Engine-1.0.6 usage
Unity publishing /build settings
【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
What kind of answer has Inspur given in the big AI model landing test?
Responding with flow layout
图扑软件基于钻孔数据的三维地质模型可视化
Qt之QListView的简单使用(含源码+注释)
mmcv常用API介绍
/Path/to/ idiom, not a command
Word frequency statistics (string, list)
[typescript] experimentaldecorators of vscode stepping pit
使用码云PublicHoliday项目判断某天是否为工作日
Redistemplate common method summary
Responsive flow layout