.Net Maui writing to Xaml file.

Tom Meier 220 Reputation points
2024-07-03T16:12:26.7433333+00:00

I would like the user to be able to modify the XAML to fit their visual needs for example make text larger. And then be able to save that setting and reload the XAML file for that particular user. I get a compiler Error saying the name 'XamlWriter' does not exist in the current context.

//Create the Button

Button button= new Button();
button.height=50;
button.Width=100;
button.Background=Brushes.AliceBlue;
button.Content="Click Me";


// Save the Button to a string

string savedButton=XamlWriter.Save(button);

//Load the button

StringReader stringReader=new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton=(Button)XamlReader.Load(xmlReader);

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,240 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,656 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2024-07-03T22:43:41.1633333+00:00

    XamlWriter is not supported by maui.

    You will need to use an xmlwriter to create a uncompiled Xaml form as xml. Also Xaml forms are compiled for Maui, you will probably need the Xaml forms just as xml to be able to read.

    you then use the Xaml extension method LoadFromXaml() to parse and load an xml string as a Xaml form.

    https://learn.microsoft.com/en-us/dotnet/maui/xaml/runtime-load?view=net-maui-8.0

    Your approach seems wrong. you should be using Maui themes.


0 additional answers

Sort by: Most helpful