Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App.xaml does not generate initialize method when only resource dictionary applied using MVVM locator #2926

Open
marik91 opened this issue Apr 23, 2020 · 2 comments · May be fixed by #8853
Open
Labels
Bug Product bug (most likely) 🚧 work in progress
Milestone

Comments

@marik91
Copy link

marik91 commented Apr 23, 2020

  • .NET Core Version: 3.1
  • Visual Studio version: VS 16.5.4
  • Windows version: 18363.778
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Not tested

Problem description:
when using MVVM pattern StartupUri property is deleted from app.xaml, instead in application.resource resource dictionary is added and static resource for view model locator is added. then resolving view and trying to call show on it will throw runtime exception "Cannot find resource named 'Locator'. Resource names are case sensitive.". Turns out when only single static resource is added InitializeComponent method is not generated. Work around inside resource dictionary add any mapping to merged resource dictionary then InitializeComponent method is generated and application works as expected

Expected behavior:
When added locator resource, InitializeComponent method should be generated

Minimal repro:
Was using Host class to enable core IoC and Mvvmlight
// App.xaml: not generating InitializeComponent

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             >
    <Application.Resources>
         <ResourceDictionary>
             <local:ViewModelLocator x:Key="Locator" />
         </ResourceDictionary>
    </Application.Resources>
</Application>

// App.xaml generating InitializeComponent

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             >
    <Application.Resources>
         <ResourceDictionary>
             <local:ViewModelLocator x:Key="Locator" />
             <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="styles.xaml" /> <!--empty resource dictionary -->
            </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
    </Application.Resources>
</Application>

// MainWindow.xaml

<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"
        DataContext="{Binding Source={StaticResource Locator}, Path=MainViewModel}"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

// ViewModelLocator

public class ViewModelLocator
{
    public MainViewModel MainViewModel => App.ServiceProvider.GetRequiredService<MainViewModel>();
}
@rladuca
Copy link
Member

rladuca commented Apr 23, 2020

@dotnet/wpf-developers This reminds me of #2543. That reproduced in .NET 4.8 as well, it's likely a long-standing issue with PresentationBuildTasks generating code.

@fabiant3 fabiant3 added the Bug Product bug (most likely) label May 19, 2020
@fabiant3 fabiant3 added this to the Future milestone May 19, 2020
ThomasGoulet73 added a commit to ThomasGoulet73/wpf that referenced this issue Feb 29, 2024
@ThomasGoulet73
Copy link
Contributor

This bug should be fixed by #8853. For now, you can workaround this issue by adding a dummy item in <Application.Resources>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Product bug (most likely) 🚧 work in progress
Projects
None yet
4 participants