模式代码从MainWindow.xaml.cs到MainView.cs [英] Mode code from MainWindow.xaml.cs to MainView.cs
问题描述
我有代码后面的行。
MainViewModel _dataContext;
public MainWindow()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException + = new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
初始化();
_dataContext = new MainViewModel();
this .DataContext = _dataContext;
}
关于初始化
,它非常冗长。
public void 初始化( )
{
try
{
string logFolder = C:\\Test;
string logName = IvrApplication.Log 跨度>;
if (!Directory.Exists(System.IO.Path.Combine(logFolder,logName)))
Directory.CreateDirectory (logFolder);
ConcurrentLog = new 日志(System.IO.Path.Combine(logFolder,logName));
System.Net.IPAddress [] ips = System.Net.Dns.GetHostAddresses(ConfigurationManager.AppSettings [ veServer跨度>]);
if (ips == null || ips.Length == 0 ) throw new 异常( 错误:无法解析指定的Telephony Server!);
string sIpaddress = @ gtcp:/ / + ips [ 0 ]。ToString();
ConcurrentLog.Write( 正在连接到:{0},sIpaddress);
ts = new TelephonyServer(sIpaddress, username, password);
ts.ConnectionLost + = new ConnectionLost(TelephonyServer_ConnectionLost);
ts.ConnectionRestored + = new ConnectionRestored(TelephonyServer_ConnectionRestored);
ServerInfo serverInfo = ts.GetServerInfo();
portCount = serverInfo.SipChannelCount;
if (portCount == 0 )
{
MessageBoxResult结果= MessageBox.Show( 请关闭此窗口。, 确认,MessageBoxButton.YesNo,MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
btnStart.IsEnabled = false ; // 按钮已禁用
btnStart.Background = Brushes.Red;
btnCancel.IsEnabled = false ; // 按钮已禁用
btnCancel.Background = Brushes.Red;
}
catch (例外情况)
{
ConcurrentLog.Write( 异常: + ex.Message + \\\\ n + ex.StackTrace);
}
}
因为我使用MVVM模式,所以代码喜欢:
public class MainViewModel:NotifyUIBase
{
private string serverString;
public string ServerString
{
get { return serverString; }
set
{
serverString = value ;
RaisePropertyChanged();
}
}
public ObservableCollection< Calls> items = new ObservableCollection< Calls>();
public ObservableCollection< Calls>商品
{
获取 {返回项; }
set
{
items = value ;
RaisePropertyChanged();
}
}
现在我的问题是我不想在代码背后有太多代码,我想移动方法初始化
到 MainViewModel
。
我能和怎么做? div class =h2_lin>解决方案
你可以在视图上使用绑定,在viewmodel上创建一个属性并将其绑定到你想要处理的属性,就像这样...... >
私人刷子buttonBackground = Brushes.Grey; // 或您想要的任何画笔......
public Brush ButtonBackground
{
get { return buttonBackground ; }
set
{
buttonBackground = value ;
RaisePropertyChanged();
}
}
并将其绑定到按钮Background属性,如下所示...
< 按钮 背景 = {Binding ButtonBackground} ... = / >
和已启用属性,您可以使用ICommand并将其放在CanExecute方法上。 />
编辑:
如果您不想导入System.Windows.Media命名空间,请将颜色用作十六进制字符串并使用关于绑定的IValueConverter。
I have the lines in code behind.
MainViewModel _dataContext;
public MainWindow()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Initialization();
_dataContext = new MainViewModel();
this.DataContext = _dataContext;
}
About Initialization
, it pretty lengthy.
public void Initialization()
{
try
{
string logFolder = "C:\\Test";
string logName = "IvrApplication.Log";
if (!Directory.Exists(System.IO.Path.Combine(logFolder, logName)))
Directory.CreateDirectory(logFolder);
ConcurrentLog = new Log(System.IO.Path.Combine(logFolder, logName));
System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(ConfigurationManager.AppSettings["veServer"]);
if (ips == null || ips.Length == 0) throw new Exception("Error: Could not resolve Telephony Server specified!");
string sIpaddress = @"gtcp://" + ips[0].ToString();
ConcurrentLog.Write("Connecting to: {0}", sIpaddress);
ts = new TelephonyServer(sIpaddress, "username", "password");
ts.ConnectionLost += new ConnectionLost(TelephonyServer_ConnectionLost);
ts.ConnectionRestored += new ConnectionRestored(TelephonyServer_ConnectionRestored);
ServerInfo serverInfo = ts.GetServerInfo();
portCount = serverInfo.SipChannelCount;
if (portCount == 0)
{
MessageBoxResult result = MessageBox.Show("Please close this window.", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
btnStart.IsEnabled = false; // button disabled
btnStart.Background = Brushes.Red;
btnCancel.IsEnabled = false; // button disabled
btnCancel.Background = Brushes.Red;
}
catch (Exception ex)
{
ConcurrentLog.Write("Exception: " + ex.Message + "\r\n" + ex.StackTrace);
}
}
Because I use MVVM pattern,so the code likes:
public class MainViewModel : NotifyUIBase
{
private string serverString;
public string ServerString
{
get { return serverString; }
set
{
serverString = value;
RaisePropertyChanged();
}
}
public ObservableCollection<Calls> items = new ObservableCollection<Calls>();
public ObservableCollection<Calls> Items
{
get { return items; }
set
{
items = value;
RaisePropertyChanged();
}
}
Now my question is that I don't want too many code in code behind, I want to move the method Initialization
to MainViewModel
.
Can I and how?
You could use binding on the view, create a property on the viewmodel and bind it to the properties you want to handle, something like this...
private Brush buttonBackground = Brushes.Grey; // or whatever brush you want... public Brush ButtonBackground { get { return buttonBackground; } set { buttonBackground= value; RaisePropertyChanged(); } }
and bind it to the button "Background" property like this...
<button background="{Binding ButtonBackground}" ...="" />
and for the "Enabled" property, you may use an ICommand and put that on the CanExecute method.
edit:
If you don't want to import the System.Windows.Media namespace, use the color as a hex string and use a IValueConverter on the binding.
这篇关于模式代码从MainWindow.xaml.cs到MainView.cs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!