Skip to content

Problem with loading component in WPF project #1688

Open
@grubioe

Description

@grubioe

@ds1709 commented on Wed Aug 14 2019

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.


@carlossanlop commented on Wed Aug 14 2019

// Exception: Cannot register duplicate Name 'Foo' in this scope.

@ds1709 thank you for the details.

@grubioe can you help with this issue? Feel free to transfer it to the dotnet/wpf repo if necessary.


@grubioe commented on Wed Aug 14 2019

@diverdan92 - this might be more related with VS, maybe in the project system, the code generator. Can you route to the right team?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions