Issue
I am having a DialogView class that is derived from a base class Dialog which is again derived from ContentView, ie. DialogView : Dialog : ContentView. I am getting errors that are mentioned later.
Here is my XAML code (DialogView.xaml)
<controls:DialogView xmlns="http://xamarin.com/schemas/2014/forms"
...
xmlns:controls="clr-namespace:MyProject.Views.Controls"
IsVisible="False"> <!--error: The property 'IsVisible' was not found in 'DialogView'-->
<ContentView.Content> <!--error: The attachable property 'Content' was not found in 'ContentView'
...
</ContentView.Content>
</controls:DialogView>
Here is my C# Code (DialogView.xaml.cs)
public class Dialog : ContentView
{
// Some properties
}
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DialogView : Dialog // error: Partial declarations of View must not specify different base classes
{
}
Errors:
DialogViewclass is a partial class that has view defined in XAML. I want to accessIsVisibleproperty of it in the XAML, but I cannot access it, I am getting error: The property 'IsVisible' was not found in DialogView.IsVisibleis a property ofVisualElementwhich is available inContentViewas it is derived fromVisualElementat multiple levels then why is it not available inDialogViewasDialogViewis derived fromContentView.I also cannot use
<ContentView.Content>insideDialogView's XAML. I am getting an error: The attachable property 'Content' was not found in 'ContentView'.I am also getting error Partial declarations of View must not specify different base classes where I am defining
public partial class DialogView : Dialog.
In C#, public members of base classes are directly accessible in the derived class as derived class and base class share an is-a relationship. Is that not a case in XAML? How can I access base class's members in derived class while in XAML.
Solution
Just noticed something. You say that the xaml is in DialogView.xaml. If that's correct, then you've made a mistake.
The root element's type must be the parent class. Use x:Class to declare the class itself:
<controls:Dialog xmlns...
x:Class="YourNamespace.DialogView">
ALTERNATIVE SOLUTION
Its possible that you are overcomplicating the situation.
Why do you define both Dialog and DialogView classes, in DialogView.xaml.cs? That is possible, but almost certainly does not do what you want.
Instead do this:
Add a new
ContentViewto your project, namedDialog. This will add two files,Dialog.xamlandDialog.xaml.cs. In those files, put everything you want to exist in all your subclasses ofDialog.Add another new
ContentViewto your project, namedDialogView. This will add two more files,DialogView.xamlandDialogView.xaml.cs. In the xaml, change<ContentView ...to<controls:Dialog .... In the cs, changepublic partial DialogView : ContentViewtopublic partial DialogView : Dialog. Thus, your DialogView is now a Dialog. Build Solution (so intellisense will correctly see the parent change from ContentView to Dialog.) Now add to xaml and cs as desired.
NOTE: At some time in the future (or maybe its already possible in 2022 Preview), I expect it will become possible for VS to directly create a xaml-based control that inherits from your custom view class. Then you won't have to do a kludge like I do above for DialogView, where I first create a ContentView, and then rename the parent class.
Answered By - ToolmakerSteve
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.