Problem with XAMLReader

BitSmithy 2,141 Reputation points
2024-04-30T10:27:51.93+00:00

Hello,

I have a xaml file in my Assets folder "MainPage1.xaml" defined as below.

<StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="using:Label"
>
    <Grid local:PageControl.PageHeight="10">
    </Grid>
</StackPanel>

Next, I am trying to load this file to my app using XAMLReader

private async void Page_Loaded(object sender, RoutedEventArgs e) 
{ 
	var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");             
	string xamlDefinition = await Windows.Storage.FileIO.ReadTextAsync(await folder.GetFileAsync("MainPage1.xaml"));           
	var xamlElement = ((FrameworkElement)XamlReader.Load(xamlDefinition)); 
}

I get such error:

Windows.UI.Xaml.Markup.XamlParseException:

The attachable property 'PageHeight' was not found in type 'PageControl'. [Line: 5 Position: 11]”

PageControl class is defined as below:

public class PageControl : DependencyObject 
{          
public static readonly DependencyProperty PageHeightProperty =     DependencyProperty.RegisterAttached(         "PageHeight",
        typeof(double),
        typeof(PageControl),
        new PropertyMetadata(default(double))
    );

        public static void SetPageHeight(UIElement element, double value)
        {
            element.SetValue(PageHeightProperty, value);
        }

        public static double GetPageHeight(UIElement element)
        {
            return (double)element.GetValue(PageHeightProperty);         	
}  
     
}//END PageControl 

I did some tests and behavior of this issue is strange:
If, I move the code to newly created app this code works.
In previous version of my app this code was working.
In debug mode it works, in release mode it doesnt work.

My app is too big to share it, so please guess where problem can lies.
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 19,646 Reputation points Microsoft Vendor
    2024-05-17T01:56:35.22+00:00

    Hi @BitSmithy ,

    1>C:\Users......\Properties\Default.rd.xml(29): warning : ILTransform: warrning ILT0027: Property Element „PageHeight” can't be found in „MyNamespace.PageControl” element. 1>C:\Users.......\Properties\Default.rd.xml(30): warning : ILTransform: warrning ILT0027: Property Eleemnt „PageHeightProperty” can't be found in „MyNamespace.PageControl” element.

    Here is the workaround, use local:PageControl.PageHeight first in MainPage.xaml or other non-XamlReader.loaded object.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.