0
wpfに2つのユーザーコントロールがあり、両方とも単純な子ウィンドウが定義されています。Uc1にはchild1、Uc2にはchild2があります。コードの背後からUc1の逆もまた同様です。他のUserControlからSimpleChildWindowにアクセスする方法
wpfに2つのユーザーコントロールがあり、両方とも単純な子ウィンドウが定義されています。Uc1にはchild1、Uc2にはchild2があります。コードの背後からUc1の逆もまた同様です。他のUserControlからSimpleChildWindowにアクセスする方法
私のやり方は、メインウィンドウからです。両方のコントロール間に依存関係を作成する必要はありません。これは、ユーザーコントロールを再利用しているためです。
これはイベントを通じて子を渡す例です。私はUserControlのコンストラクタへの参照としてmainwindowを渡すことはありません。
これを行うには、インターフェイスを作成してMainWindowに実装し、インターフェイスとして渡す必要があります。
等:
UC1-(event)>
メインウィンドウ-(methodcall)>
UC2-(methodcall)>
UC2.child
擬似コード:
// event args.
public class RequestChildEventArgs : EventArgs
{
public Child2 Child { get;set; }
}
public class UC1
{
// do something when you need the child2
public void DoSomething()
{
var child2 = GetChild2();
if(child2 == null)
// cry.
}
// this method requests a reference of child2
private Child2 GetChild2()
{
// check if the event is assigned.
if(RequestChild == null)
return null;
RequestChildEventArgs args = new RequestChildEventArgs();
RequestChild2(this, args);
return args.Child;
}
public event EventHandler<RequestChildEventArgs> RequestChild2;
}
// user control 2
public class UC2
{
public Child2 Child2 { get; } = new Child2();
}
// the mainwindow that tunnels the Child2
public class MainWindow
{
private UC1 _uc1;
private UC2 _uc2;
public MainWindow()
{
_uc1 = new UC1();
_uc2 = new UC2();
_uc1.RequestChild2 += (s, e) => e.Child = _uc2.Child2;
}
}
その逆バージョン:
擬似コード:
// event args.
public class RequestChildEventArgs<T> : EventArgs
{
public T Child { get; set; }
}
public class UC1
{
public Child1 Child1 { get; } = new Child1();
// do something when you need the child2
public void DoSomething()
{
var child2 = GetChild2();
if (child2 == null)
// cry.
}
// this method requests a reference of child2
private Child2 GetChild2()
{
// check if the event is assigned.
if(RequestChild2 == null)
return null;
RequestChildEventArgs<Child2> args = new RequestChildEventArgs<Child2>();
RequestChild2(this, args);
return args.Child;
}
public event EventHandler<RequestChildEventArgs<Child2>> RequestChild2;
}
// user control 2
public class UC2
{
public Child2 Child2 { get; } = new Child2();
// do something when you need the child1
public void DoSomething()
{
var child1 = GetChild1();
if(child1 == null)
// cry.
}
// this method requests a reference of child1
private Child1 GetChild1()
{
// check if the event is assigned.
if(RequestChild1 == null)
return null;
RequestChildEventArgs<Child1> args = new RequestChildEventArgs<Child1>();
RequestChild1(this, args);
return args.Child;
}
public event EventHandler<RequestChildEventArgs<Child1>> RequestChild1;
}
// the mainwindow that tunnels the Childs
public class MainWindow
{
private UC1 _uc1;
private UC2 _uc2;
public MainWindow()
{
_uc1 = new UC1();
_uc2 = new UC2();
_uc1.RequestChild2 += (s, e) => e.Child = _uc2.Child2;
_uc2.RequestChild1 += (s, e) => e.Child = _uc1.Child1;
}
}
この方法は、あなたがメインウィンドウまたはシングルトンオブジェクトでない dependendをユーザーコントロールです。
ここにコードを投稿してください。それは私のために本当に役立つだろう! –
本当にありがとう!それは私のために動作します! –