C
C#15mo ago
r2d25551

❔ How do I pass in the parent ContentView to a class contained in the XAML? Is it possible?

For example:
<ContentView x:Class="MyView>
<local:MyClass>
<x:Arguments>
<ContentView />
</x:Arguments>
</local:MyClass>
<ContentView x:Class="MyView>
<local:MyClass>
<x:Arguments>
<ContentView />
</x:Arguments>
</local:MyClass>
And have the constuctor of MyClass get the type MyView.
public class MyClass
{
public MyClass(object obj)
{
MyView myView = obj as MyView;
:
}
}
public class MyClass
{
public MyClass(object obj)
{
MyView myView = obj as MyView;
:
}
}
Also tried variations of <x:Arguments> to include the x:Class="", ...
<x:Arguments>
<local:MyView />
</x:Arguments>
<x:Arguments>
<local:MyView />
</x:Arguments>
5 Replies
r2d25551
r2d25551OP15mo ago
With ContentView as the argument type it does get me a little closer but the object passed in to my constructor is not of the type I was expecting.
JakenVeina
JakenVeina15mo ago
this seems like a horrific design pattern but, uhhh so, you want MyClass to be given the instance of ContentView that it belongs to?
<ContentView
x:Class="MyView"
x:Name="This">
<local:MyClass>
<x:Arguments>
<x:Reference Name="This"/>
</x:Arguments>
</local:MyClass>
</ContentView>
<ContentView
x:Class="MyView"
x:Name="This">
<local:MyClass>
<x:Arguments>
<x:Reference Name="This"/>
</x:Arguments>
</local:MyClass>
</ContentView>
this, I think, should do it if not, I dunno but I can almost guarantee you there's a better way to do whatever it is you're trying to do
r2d25551
r2d25551OP15mo ago
Agreed, for XAML. But done all the time in C++. This is a new framework to me and there are a few things that would be done better. My "control" is derived from GraphicsView, and the control is used by XAML ContentView. "Parent" is the enclosing view, which was null at time the constructor is called. That design probably should be looked at more than once. If Parent is null then there is no parent. I ended up using the Loaded event of the view. Thank you for taking the time to propose a solution.
JakenVeina
JakenVeina15mo ago
it's not really a XAML/C++ thing it's the fact that it looks like you're mixing layers, and thus mixing responsibilities if you feel like explaining your scenario more, I can probably provide more context on what a "good" pattern would be
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server