如何将另一个 class 的对象发送给自己?
How to send an object of another class to itself?
我是 WPF 的新手,无法理解这个问题,所以检查了几个教程并将它们合并在一起,我在这里遇到了这个烂摊子。真的不知道该如何描述我的问题,所以我决定写在这里。
我在做什么;
使用 WPF,我在 Main Window 中获得了一个打开 UserControls 的菜单。
当 Main Window 启动时,我正在启动一个这样的线程,所以我得到一个循环
private void MainLoop()
{
Thread th = Thread.CurrentThread;
while (th.ThreadState != ThreadState.AbortRequested &&
th.ThreadState != ThreadState.Aborted)
{
if (bMainOk)
{
switch (activeUC)
{
case "ucDurum":
LoopControl.ucDurumLoop(plc, connectionString);
break;
case "ucAyarlar":
//LoopControl.ucAyarlarLoop();
break;
default:
break;
}
}
Thread.Sleep(2000);
}
}
LoopControl class 的内部函数根据活动的 UserControl 循环。
public class LoopControl : ObservableObject
{
public static void ucDurumLoop(ActUtlType plc, string connectionString)
{
DurumVM durum = new DurumVM();
// The values do change before this lines, I didnt put them for the sake of clarity
durum.RaporAktif = "true";
durum.RaporAdedi = arrayData[0].ToString();
durum.BufferPercent = "%" + (float)arrayData[0] / 250 * 100;
durum.PlcSonCounter = arrayData[4].ToString();
durum.SqlSonCounter = sonCntr.ToString();
}
}
连接到主 ViewModel,然后像这样子 ViewModels
class MainVM
{
public DurumVM Durum { get; private set; }
public AyarlarVM Ayarlar { get; private set; }
public MainVM()
{
Durum = new DurumVM();
Ayarlar = new AyarlarVM();
}
}
public class DurumVM : ObservableObject
{
#region PLCVars
private bool _raporAktif;
private string _raporAdedi;
private string _bufferPercent;
private string _plcSonCounter;
private string _sqlSonCounter;
#endregion
#region PLCGetSets
public string RaporAktif
{
get
{
if (_raporAktif)
return "Düzenlenecek True";
return "Düzenlenecek False";
}
set
{
_raporAktif = Convert.ToBoolean(value);
OnPropertyChanged("RaporAktif");
}
}
public string RaporAdedi
{
get
{
if (string.IsNullOrEmpty(_raporAdedi))
return "Zeroh";
return _raporAdedi;
}
set
{
_raporAdedi = value;
OnPropertyChanged("RaporAdedi");
}
}
public string BufferPercent
{
get
{
if (string.IsNullOrEmpty(_bufferPercent))
return "%0";
return _bufferPercent;
}
set
{
_bufferPercent = (value.Length <= 5 ? value : value.Substring(0, 5));
OnPropertyChanged("BufferPercent");
}
}
public string PlcSonCounter
{
get
{
if (string.IsNullOrEmpty(_plcSonCounter))
return "0";
return _plcSonCounter;
}
set
{
_plcSonCounter = value;
OnPropertyChanged("PlcSonCounter");
}
}
public string SqlSonCounter
{
get
{
if (string.IsNullOrEmpty(_sqlSonCounter))
return "0";
return _sqlSonCounter;
}
set
{
_sqlSonCounter = value;
OnPropertyChanged("SqlSonCounter");
}
}
#endregion
}
并且用户控件绑定到这些值:
<StackPanel>
<DockPanel>
<Label Content="Aktif mi? :" VerticalAlignment="Center"/>
<TextBlock Text="{Binding RaporAktif, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" Margin="0,0,25,0" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect/>
</TextBlock.Effect>
</TextBlock>
</DockPanel>
...
public partial class ucDurum : UserControl
{
MainVM viewModel = new MainVM();
//LoopControl viewModel = new LoopControl();
public ucDurum()
{
InitializeComponent();
this.DataContext = viewModel.Durum;
Control.activeUC = "ucDurum";
}
}
快速求和:
我得到了一组用户控件和一个主循环。主循环的部分只有在正确的用户控件处于活动状态时才有效,连接到其他地方并获取值然后 在引用上设置这些值,将其发送到用户控件并文本块绑定的值将更新
我知道有什么问题:
做法可能很不正确。它在我脑海中看起来不错,但我意识到了问题所在。主要是由于我缺乏 C# 知识。主循环获取 VM 集数据的引用,但从不将它们发送到任何地方,正如我在快速摘要中加粗的那样。我确定问题就在那里,因为我得到的值是正确的,当我更改值视图按钮等时,它们确实会更改和更新。但是VM不知道LoopControl。
在 MVC 中,我 return 引用的对象然后在视图中获取值。在这里,值是直接绑定的,我有点迷路了。
提前致谢。
更新:
很抱歉,我在最后几段上面说得不够清楚。
我希望它做什么:
LoopControl.ucDurumLoop 使用引用的对象更新 DurumVM,以便用户控件“ucDurum”可以看到它并相应地更新 TextBlock 值。
发生了什么:
我在 LoopControl.ucDurumLoop 中使用这些值设置了引用对象,但这只是一个对象并保留在那里。不会转到 DurumVM 并使用它们的 Get/Set 更新值,因此值相同并且用户控件“ucDurum”不知道 LoopControl 上发生了什么。
好吧,我是愚蠢的。
问题是,我正在创建新的对象引用并更新它,而我不得不引用 UserControl 上的对象。
我所需要的只是在 LoopControl 中更改一行
public class LoopControl : ObservableObject
{
public static void ucDurumLoop(ActUtlType plc, string connectionString)
{
DurumVM durum = new DurumVM(); // Changing this
DurumVM durum = ucDurum.viewModel; // To this.
durum.RaporAktif = "true";
durum.RaporAdedi = arrayData[0].ToString();
durum.BufferPercent = "%" + (float)arrayData[0] / 250 * 100;
durum.PlcSonCounter = arrayData[4].ToString();
durum.SqlSonCounter = sonCntr.ToString();
}
}
我是 WPF 的新手,无法理解这个问题,所以检查了几个教程并将它们合并在一起,我在这里遇到了这个烂摊子。真的不知道该如何描述我的问题,所以我决定写在这里。
我在做什么;
使用 WPF,我在 Main Window 中获得了一个打开 UserControls 的菜单。 当 Main Window 启动时,我正在启动一个这样的线程,所以我得到一个循环
private void MainLoop()
{
Thread th = Thread.CurrentThread;
while (th.ThreadState != ThreadState.AbortRequested &&
th.ThreadState != ThreadState.Aborted)
{
if (bMainOk)
{
switch (activeUC)
{
case "ucDurum":
LoopControl.ucDurumLoop(plc, connectionString);
break;
case "ucAyarlar":
//LoopControl.ucAyarlarLoop();
break;
default:
break;
}
}
Thread.Sleep(2000);
}
}
LoopControl class 的内部函数根据活动的 UserControl 循环。
public class LoopControl : ObservableObject
{
public static void ucDurumLoop(ActUtlType plc, string connectionString)
{
DurumVM durum = new DurumVM();
// The values do change before this lines, I didnt put them for the sake of clarity
durum.RaporAktif = "true";
durum.RaporAdedi = arrayData[0].ToString();
durum.BufferPercent = "%" + (float)arrayData[0] / 250 * 100;
durum.PlcSonCounter = arrayData[4].ToString();
durum.SqlSonCounter = sonCntr.ToString();
}
}
连接到主 ViewModel,然后像这样子 ViewModels
class MainVM
{
public DurumVM Durum { get; private set; }
public AyarlarVM Ayarlar { get; private set; }
public MainVM()
{
Durum = new DurumVM();
Ayarlar = new AyarlarVM();
}
}
public class DurumVM : ObservableObject
{
#region PLCVars
private bool _raporAktif;
private string _raporAdedi;
private string _bufferPercent;
private string _plcSonCounter;
private string _sqlSonCounter;
#endregion
#region PLCGetSets
public string RaporAktif
{
get
{
if (_raporAktif)
return "Düzenlenecek True";
return "Düzenlenecek False";
}
set
{
_raporAktif = Convert.ToBoolean(value);
OnPropertyChanged("RaporAktif");
}
}
public string RaporAdedi
{
get
{
if (string.IsNullOrEmpty(_raporAdedi))
return "Zeroh";
return _raporAdedi;
}
set
{
_raporAdedi = value;
OnPropertyChanged("RaporAdedi");
}
}
public string BufferPercent
{
get
{
if (string.IsNullOrEmpty(_bufferPercent))
return "%0";
return _bufferPercent;
}
set
{
_bufferPercent = (value.Length <= 5 ? value : value.Substring(0, 5));
OnPropertyChanged("BufferPercent");
}
}
public string PlcSonCounter
{
get
{
if (string.IsNullOrEmpty(_plcSonCounter))
return "0";
return _plcSonCounter;
}
set
{
_plcSonCounter = value;
OnPropertyChanged("PlcSonCounter");
}
}
public string SqlSonCounter
{
get
{
if (string.IsNullOrEmpty(_sqlSonCounter))
return "0";
return _sqlSonCounter;
}
set
{
_sqlSonCounter = value;
OnPropertyChanged("SqlSonCounter");
}
}
#endregion
}
并且用户控件绑定到这些值:
<StackPanel>
<DockPanel>
<Label Content="Aktif mi? :" VerticalAlignment="Center"/>
<TextBlock Text="{Binding RaporAktif, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" Margin="0,0,25,0" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect/>
</TextBlock.Effect>
</TextBlock>
</DockPanel>
...
public partial class ucDurum : UserControl
{
MainVM viewModel = new MainVM();
//LoopControl viewModel = new LoopControl();
public ucDurum()
{
InitializeComponent();
this.DataContext = viewModel.Durum;
Control.activeUC = "ucDurum";
}
}
快速求和: 我得到了一组用户控件和一个主循环。主循环的部分只有在正确的用户控件处于活动状态时才有效,连接到其他地方并获取值然后 在引用上设置这些值,将其发送到用户控件并文本块绑定的值将更新
我知道有什么问题: 做法可能很不正确。它在我脑海中看起来不错,但我意识到了问题所在。主要是由于我缺乏 C# 知识。主循环获取 VM 集数据的引用,但从不将它们发送到任何地方,正如我在快速摘要中加粗的那样。我确定问题就在那里,因为我得到的值是正确的,当我更改值视图按钮等时,它们确实会更改和更新。但是VM不知道LoopControl。
在 MVC 中,我 return 引用的对象然后在视图中获取值。在这里,值是直接绑定的,我有点迷路了。
提前致谢。
更新: 很抱歉,我在最后几段上面说得不够清楚。
我希望它做什么: LoopControl.ucDurumLoop 使用引用的对象更新 DurumVM,以便用户控件“ucDurum”可以看到它并相应地更新 TextBlock 值。
发生了什么: 我在 LoopControl.ucDurumLoop 中使用这些值设置了引用对象,但这只是一个对象并保留在那里。不会转到 DurumVM 并使用它们的 Get/Set 更新值,因此值相同并且用户控件“ucDurum”不知道 LoopControl 上发生了什么。
好吧,我是愚蠢的。
问题是,我正在创建新的对象引用并更新它,而我不得不引用 UserControl 上的对象。
我所需要的只是在 LoopControl 中更改一行
public class LoopControl : ObservableObject
{
public static void ucDurumLoop(ActUtlType plc, string connectionString)
{
DurumVM durum = new DurumVM(); // Changing this
DurumVM durum = ucDurum.viewModel; // To this.
durum.RaporAktif = "true";
durum.RaporAdedi = arrayData[0].ToString();
durum.BufferPercent = "%" + (float)arrayData[0] / 250 * 100;
durum.PlcSonCounter = arrayData[4].ToString();
durum.SqlSonCounter = sonCntr.ToString();
}
}