Skip to content

Problem with loading component in WPF project #3189

Closed
@ds1709

Description

@ds1709

Issue Title

Problem with loading component in WPF project by calling Application.LoadComponent(Uri).

General

Version: .NET Core 3.0.0 Preview 8.
OS: Windows 10.
Visual Studio: 2019 16.2.1.
Project Sdk: Microsoft.NET.Sdk.WindowsDesktop.

In the next scenario Application.LoadComponent(Uri) throws exception.
Created a simple WPF application with classe App and MainWindow.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <UseWPF>true</UseWPF>
    <AssemblyVersion>1.2.3.54</AssemblyVersion>
  </PropertyGroup>
</Project>
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Border x:Name="Foo" />
    </Grid>
</Window>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var uri = new Uri("/WpfApp1;component/mainwindow.xaml", UriKind.Relative);
        var window = LoadComponent(uri); // Exception: Cannot register duplicate Name 'Foo' in this scope.
    }
}

Key points here are <AssemblyVersion> in csproj file and named element in MainWindow xaml file. When it's declared, the window code generator creates code like System.Uri resourceLocater = new System.Uri("/WpfApp1;V1.2.3.54;component/mainwindow.xaml", System.UriKind.Relative);. As you can see, there's version element in URI. But when <AssemblyVersion> is not declared, or version is declared in code ([assembly:AssemblyVersion("...")]), then version is missing in generated URI, and exception is not throwing. Same for UserControls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-wpfIssues related to WPF in .NET Core

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions