[英]Inheriting a list from another WPF window
我有一個已經構建了一個主窗口的程序,我正在添加一個名為“Profiler”的窗口。 主窗口中有一個我需要能夠使用的列表,因此我需要使用主窗口作為基礎。
我做的第一件事是在主窗口類中創建了我需要保護的列表並創建並獲取方法:
protected List<PaperRoll> paperRolls = new List<PaperRoll>();
public List<PaperRoll> PaperRolls
{
get { return paperRolls; }
}
然后在探查器窗口中,我嘗試使用主窗口(稱為 DspWindow)作為基類:
public partial class Profiler : DspWindow
{
public Profiler() : base ()
{
InitializeComponent();
List<PaperRoll> pr = base.PaperRolls;
}
}
這導致了錯誤Partial declarations of 'Profiler' must not specified different base classes 。 我試圖研究這個錯誤,它似乎是因為 xaml 不是從基類繼承的。 因此,我更改為 xaml 以包含基類:
<base:DspWindow x:Class="DSP_Simulator.Profiler"
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:DSP_Simulator"
mc:Ignorable="d"
Title="Profiler" Height="300" Width="300">
<Grid>
</Grid>
</base:DspWindow>
這導致了多個錯誤,例如未找到 'base:DspWindow' 、未定義前綴 'base'和'base' 是未聲明的前綴。 請問有人能告訴我我要去哪里嗎?
您似乎不了解繼承的工作原理。
僅僅因為您想在對象中使用類的成員並不意味着您必須從它繼承。
我認為您不想使用定義而是使用列表的值,因此我建議您不要從DspWindow
繼承,而應該在Profiler
引用它。
我不知道你的窗口是如何一起工作的,但我猜DspWindow
創建了一個新的Profiler
,所以我建議你可以創建一個新的Profiler
對象,它的構造函數接收DspWindow
作為參數,然后在內部使用它的實例。
public partial class Profiler : Window
{
private DspWindow dspWindow;
public Profiler(DspWindow dspWindow)
{
InitializeComponent();
this.dspWindow = dspWindow;
}
void DoSomething()
{
int numberOfPaperRolls = dspWindow.PaperRolls.Count; //you can access the list in this object aswell!
}
}
在DspWindow
:
var prof = new Profiler(this);
prof.Show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.