-
I saw something like this on XAML:
How to convert this to the C# markup style: I want to define the styles such that once assigned to an UI element it take cares of platform differences. I don't need to define different styles for different platforms. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Declarative (XAML) and procedural languages (C#) have very fundamental differences and if you try and directly port one to the other you'll get bad code. Don't try and write the direct C# equivalent of XAML. In doing that you might come up with the idea to use a factory method, or something similar. private Button CreateButton(string text)
{
var btn = new Button().Text(text);
// ... Add other properties as appropriate
#if ANDROID
btn.CornerRadius = 40;
#elif IOS
btn.CornerRadius = 20;
#endif
return btn;
} Then, whenever you need a button, you call
|
Beta Was this translation helpful? Give feedback.
-
Hey @HobDev! You can see how we're using You need to create a |
Beta Was this translation helpful? Give feedback.
-
Using custom classes is a very good idea. It decreases the complexity. |
Beta Was this translation helpful? Give feedback.
-
It seems like it's not possible to ditch the styles altogether. I am themeing the app with styles:
Now when I want to use custom classes I can't use |
Beta Was this translation helpful? Give feedback.
<OpinionThatIWouldLikeFeedbackOn>
Styles are a XAML thing. Mixing them and C# Markup has the potential to give you a codebase that is still a mix of the two.
Declarative (XAML) and procedural languages (C#) have very fundamental differences and if you try and directly port one to the other you'll get bad code.
Don't try and write the direct C# equivalent of XAML.
Instead, ask yourself "How (in C#) would I create multiple types which have the same encapsulated logic?"
In doing that you might come up with the idea to use a factory method, or something similar.
So, you might have something like: