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

Rewrite WPF pack-URIs found in CLR strings #387

Open
warappa opened this issue Jan 30, 2025 · 2 comments
Open

Rewrite WPF pack-URIs found in CLR strings #387

warappa opened this issue Jan 30, 2025 · 2 comments

Comments

@warappa
Copy link
Contributor

warappa commented Jan 30, 2025

Issue

I'm developing a WPF application which uses WPF.UI.

With the newest ILRepack version (2.0.36) the XAML/BAML rewrite works great!
But unfortunately, WPF.UI tends to use hardcoded pack-uris in its code behind files (normal CLR strings). Those are not found/detected by ILRepack.

Eg.
https://github.com/lepoco/wpfui/blob/397739b85cfa616315740a360279f301847fa59d/src/Wpf.Ui/Controls/ContextMenu/ContextMenuLoader.xaml.cs#L45-L51

ResourceDictionary resourceDict =
    new()
    {
        Source = new Uri("pack://application:,,,/Wpf.Ui;component/Controls/ContextMenu/ContextMenu.xaml")
    };

Expectation

Could you try to generally detect "pack:..." strings and rewrite those also (so not only specific to WPF.UI)?

Info

.NET Framework 4.8
ILRepack 2.0.36
WPF.UI 4.0.0-rc.3

@warappa
Copy link
Contributor Author

warappa commented Jan 30, 2025

I got a POC working:
In XamlResourcePathPatcherStep change/add the following:

public void Perform()
{
    var types = _repackContext.TargetAssemblyDefinition.Modules.SelectMany(m => m.Types);

    _logger.Verbose("Processing XAML resource paths ...");
    foreach (var type in types)
    {
        PatchIComponentConnector(type);
        PatchWpfToolkitVersionResourceDictionary(type);
        PatchWpfPackStrings(type); // <-- Add this line
    }
}

// Add this method
private void PatchWpfPackStrings(TypeDefinition type)
{
    foreach (var method in type.Methods.Where(x => x.HasBody))
    {
        PatchMethod(method);
    }
}

This already is enough to find and patch all pack-uris and the application does not crash anymore but works as intended!

@KirillOsenkov
Copy link
Collaborator

Once you finalize on a solution that works for you, consider sending the PR. Unfortunately I don't have the resources to do it myself, but I could help with merging the PR and shipping an update.

warappa added a commit to warappa/il-repack that referenced this issue Feb 3, 2025
warappa added a commit to warappa/il-repack that referenced this issue Feb 6, 2025
warappa added a commit to warappa/il-repack that referenced this issue Feb 7, 2025
… if pack-uris are not patched in CLR strings
warappa added a commit to warappa/il-repack that referenced this issue Feb 7, 2025
warappa added a commit to warappa/il-repack that referenced this issue Feb 9, 2025
KirillOsenkov added a commit that referenced this issue Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants