weixin_39518840
weixin_39518840
2021-01-10 04:05

How do I fix an issue with Reflection MissingMetaDataException using UWP Microsoft.Toolkit.Uwp.UI.Extensions FrameworkElement.FindChildByName?

I am using code-behind to retrieve a FrameworkElement in a UWP app using Uwp.UI.Extensions. It all works great in debug mode but on release mode. I get the following error:

This object cannot be invoked because it was metadata-enabled for browsing only:

'Windows.UI.Xaml.Controls.TextBlock.Inlines' For more information, please visit http://go.microsoft.com/fwlink/?LinkID=616867 at Internal.Reflection.Core.Execution.ExecutionEnvironment.GetMethodInvoker(RuntimeTypeInfo, QMethodDefinition, RuntimeTypeInfo[], MemberInfo) + 0x347 at SharedLibrary!+0x1290e8f at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x4c at System.Reflection.Runtime.PropertyInfos.RuntimePropertyInfo.GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) + 0x127 at System.Reflection.PropertyInfo.GetValue(Object, Object[]) + 0x56 at System.Reflection.PropertyInfo.GetValue(Object) + 0x3b at Microsoft.Toolkit.Uwp.UI.Extensions.LogicalTree.GetContentControl(FrameworkElement) + 0xdd at Microsoft.Toolkit.Uwp.UI.Extensions.LogicalTree.FindChildByName(FrameworkElement, String) + 0x395 at Microsoft.Toolkit.Uwp.UI.Extensions.LogicalTree.FindChildByName(FrameworkElement, String) + 0x1d7 at Microsoft.Toolkit.Uwp.UI.Extensions.LogicalTree.FindChildByName(FrameworkElement, String) + 0x3e4 at Microsoft.Toolkit.Uwp.UI.Extensions.LogicalTree.FindChildByName(FrameworkElement, String) + 0x1d7 at Microsoft.Toolkit.Uwp.UI.Extensions.LogicalTree.FindChildByName(FrameworkElement, String) + 0x1d7 at OpenReports.MainPage.PrintButton_Click(Object, RoutedEventArgs) + 0xfa

This is my code-behind function:

cs
var printablearea = (Grid)ContentFrame.FindChildByName("PrintableArea");
                printElement = printablearea.FindChildByName(PrintButton.Tag.ToString());
                printElement.FindChildByName("printHeader").Visibility = Visibility.Visible;

This is my XAML code for the page:

xml
<grid x:name="PrintableArea">
        <grid.columndefinitions>
            <columndefinition minwidth="{x:Bind ActualWidth}"></columndefinition>
        </grid.columndefinitions>
        <grid.rowdefinitions>
            <rowdefinition height="auto"></rowdefinition>
            <rowdefinition height="auto"></rowdefinition>
            <rowdefinition height="auto"></rowdefinition>
        </grid.rowdefinitions>
        <flyoutbase.attachedflyout>
            <menuflyout></menuflyout>
        </flyoutbase.attachedflyout>
        <canvas x:name="printingRoot" opacity="0" margin="-2000 0 0 0"></canvas>

        <dockpanel x:name="KPISelectorPanel" grid.row="0" margin="10" visibility="Visible" lastchildfill="True">
            <button x:name="KPISelectorButton" click="KPISelectorButton_Click" margin="0 0 10 0">
                <stackpanel orientation="Horizontal">
                    <symbolicon symbol="AllApps" margin="0 0 10 0"></symbolicon>
                    <textblock fontsize="20">KPIs</textblock>
                </stackpanel>
            </button>
            <navigationview x:name="KPISelector" selectionchanged="KPISelector_SelectionChanged" panedisplaymode="Top" issettingsvisible="False" isbackbuttonvisible="Collapsed" ispanetogglebuttonvisible="False"></navigationview>
        </dockpanel>

        <grid x:name="printArea" grid.row="1">
            <grid.rowdefinitions>
                <rowdefinition height="auto"></rowdefinition>
                <rowdefinition height="auto"></rowdefinition>
            </grid.rowdefinitions>

            <stackpanel x:name="printHeader" orientation="Horizontal" visibility="Collapsed" height="60" grid.row="0" horizontalalignment="Stretch">
                <image source="ms-appx:///Assets/StoreLogo.scale-400.png" stretch="Fill" horizontalalignment="Stretch" margin="5" verticalalignment="Stretch"></image>
                <headeredtextblock orientation="Vertical" header="Practice Visuals ©" text="{x:Bind local:MainPage.Version}" margin="5 5 15 5"></headeredtextblock>
                <headeredtextblock x:name="printHeaderReport" x:fieldmodifier="public" orientation="Vertical" header="Report" margin="5"></headeredtextblock>
                <headeredtextblock orientation="Vertical" header="From" text="{x:Bind mainPage.FromDatePicker.Date.Value.Date.ToLongDateString(), Mode=OneWay}" margin="5"></headeredtextblock>
                <headeredtextblock orientation="Vertical" header="To" text="{x:Bind mainPage.ToDatePicker.Date.Value.Date.ToLongDateString(), Mode=OneWay}" margin="5"></headeredtextblock>
                <headeredtextblock orientation="Vertical" header="Providers" text="{x:Bind mainPage.ProviderFilterButtonIcon.Glyph, Mode=OneWay}" margin="5"></headeredtextblock>
                <headeredtextblock orientation="Vertical" header="Procedures" text="{x:Bind mainPage.ProcedureFilterButtonIcon.Glyph, Mode=OneWay}" margin="5"></headeredtextblock>
            </stackpanel>
</grid></grid>

Interestingly enough, I have several other pages with a very similar code which is as below that works fine even in release:

xml
<grid x:name="PrintableArea">
    <grid.columndefinitions>
        <columndefinition minwidth="{x:Bind ActualWidth}"></columndefinition>
    </grid.columndefinitions>
    <grid.rowdefinitions>
        <rowdefinition height="auto"></rowdefinition>
        <rowdefinition height="auto"></rowdefinition>
        <rowdefinition height="auto"></rowdefinition>
    </grid.rowdefinitions>
    <flyoutbase.attachedflyout>
        <menuflyout></menuflyout>
    </flyoutbase.attachedflyout>
    <canvas x:name="printingRoot" opacity="0" margin="-2000 0 0 0"></canvas>

    <grid x:name="printArea" grid.row="1">
        <grid.rowdefinitions>
            <rowdefinition height="auto"></rowdefinition>
            <rowdefinition height="auto"></rowdefinition>
        </grid.rowdefinitions>

        <stackpanel x:name="printHeader" orientation="Horizontal" visibility="Collapsed" height="60" grid.row="0" horizontalalignment="Center">
            <image source="ms-appx:///Assets/StoreLogo.scale-400.png" stretch="Fill" horizontalalignment="Stretch" margin="5" verticalalignment="Stretch"></image>
            <headeredtextblock orientation="Vertical" header="Practice Visuals ©" text="{x:Bind local:MainPage.Version}" margin="5 5 15 5"></headeredtextblock>
            <headeredtextblock x:name="printHeaderReport" x:fieldmodifier="public" orientation="Vertical" header="Report" margin="5"></headeredtextblock>
            <headeredtextblock orientation="Vertical" header="From" text="{x:Bind mainPage.FromDatePicker.Date.Value.Date.ToLongDateString(), Mode=OneWay}" margin="5"></headeredtextblock>
            <headeredtextblock orientation="Vertical" header="To" text="{x:Bind mainPage.ToDatePicker.Date.Value.Date.ToLongDateString(), Mode=OneWay}" margin="5"></headeredtextblock>
            <headeredtextblock orientation="Vertical" header="Providers" text="{x:Bind mainPage.ProviderFilterButtonIcon.Glyph, Mode=OneWay}" margin="5"></headeredtextblock>
            <headeredtextblock orientation="Vertical" header="Procedures" text="{x:Bind mainPage.ProcedureFilterButtonIcon.Glyph, Mode=OneWay}" margin="5"></headeredtextblock>
        </stackpanel>
</grid></grid>

I have added the following to default.RD.xaml and made it an embedded resource but nothing has changed!

xml
<directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <application>
    <!--
      An Assembly element with Name="*Application*" applies to all assemblies in
      the application package. The asterisks are not wildcards.
    -->
    <assembly name="*Application*" dynamic="Required All"></assembly>

    <!-- Add your application specific runtime directives here. -->
    <namespace name="Microsoft.Toolkit.Uwp.UI.Extensions" serialize="Required All"></namespace>

  </application>
</directives>

What is going on here?

该提问来源于开源项目:windows-toolkit/WindowsCommunityToolkit

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39611275 weixin_39611275 4月前

    Hello novinsab, thank you for your interest in Windows Community Toolkit!

    I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible.. Other community members may also answer the question and provide feedback 🙌

    点赞 评论 复制链接分享
  • weixin_39928017 weixin_39928017 4月前

    it's not complaining about the toolkit function missing, it's complaining about Windows.UI.Xaml.Controls.TextBlock.Inlines, did you try adding that to the rd.xml file?

    That said, why are you using the tree extensions to grab an element you've already named? If you're in your page code-behind, you can just do printHeader.Visibility = Visibility.Visible? Or you could use x:Bind to bind its Visibility property to a boolean property on your page and just toggle the boolean when you want it to be visible. I think that'd be the better approach here, unless I'm missing where you're trying to execute the code from?

    点赞 评论 复制链接分享
  • weixin_39518840 weixin_39518840 4月前

    Thanks for the quick response.

    I didn’t add “Windows.UI.Xaml.Controls.TextBlock.Inlines” but I added “Windows.UI.Xaml.Controls.TextBlock” and previously “Windows.UI.Xaml.Controls” with no change. Should I try the full thing and if yes, is this a namespace or an assembly or a type? What should my rd.xml look like?

    I have to use tree extensions to grab the control as they reside on a page within the main page's contentFrame in aa Navigation View. Also the name “printHeader” may change with each page to allow a certain section to be printed.

    点赞 评论 复制链接分享
  • weixin_39518840 weixin_39518840 4月前

    I tried the following in rd.xml but no change:

    `

    <!-- Add your application specific runtime directives here. -->
    <Namespace Name="Microsoft.Toolkit.Uwp.UI.Extensions" Serialize="Required All" />
    <Namespace Name="Windows.UI.Xaml.Controls.TextBlock.Inlines" Serialize="Required All" />
    <Type Name="Windows.UI.Xaml.Controls.TextBlock.Inlines" Dynamic="Required All" />
    <Assembly Name="Windows.UI.Xaml.Controls.TextBlock.Inlines" Dynamic="Required All" />
    

    `

    点赞 评论 复制链接分享
  • weixin_39928017 weixin_39928017 4月前

    can you email dotnetnative at microsoft.com with the details? FYI .

    Do you still own each page of the app? Can they just all implement a common interface like IPrintable and you have a method that returns the content? Then you can grab the page, cast it and have your parent page controlling the content access things that way?

    点赞 评论 复制链接分享
  • weixin_39518840 weixin_39518840 4月前

    That would mean changing the whole code for printing.

    I would rather implement printing the way it is now which a parent grid is passed onto the main page through the tag and that is picked up by the print function and added as an element to print. Simple and works great.

    I'm not sure why it is only happening with pages or grids on certain pages. Could it be because these grids have an adaptive grid view in them and others don't?

    Dr. Novin S.

    点赞 评论 复制链接分享

相关推荐