如何处理 WPF 项目中生成的 .g.cs 文件中的命名空间冲突而不重命名自己的命名空间?

How to handle namespace conflicts in the .g.cs files generated in a WPF project without renaming own namespace?

提问人:Mickael Bergeron Néron 提问时间:8/23/2023 最后编辑:Mickael Bergeron Néron 更新时间:8/23/2023 访问量:34

问:

我在项目中添加了对 WPF-UI 的引用。WPF-UI 的命名空间以 Wpf 开头(即 Wpf.Ui.Controls...),而我自己的命名空间以 Wpf (Redacted.Common.Gui.Wpf) 结尾。

对于大多数文件,我可以使用 WpfUi = Wpf.Ui;

但是,在自动生成的文件.g.cs中,我无法以这种方式处理。

这是我的代码:

XAML:

<ui:UiWindow x:Class="Redacted.Common.Gui.Wpf.Views.Alert"
        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"
        xmlns:local="clr-namespace:Redacted.Common.Gui.Wpf.Views"
        xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
        mc:Ignorable="d"
        Title="Alert" Height="450" Width="800">
    <Grid>
        <ui:Button
            Content="Hello World"
            Icon="Fluent24"/>
    </Grid>
</ui:UiWindow>

代码隐藏:

using Wpf.Ui.Controls;

namespace Redacted.Common.Gui.Wpf.Views
{
    /// <summary>
    /// Interaction logic for Alert.xaml
    /// </summary>
    public partial class Alert : UiWindow
    {
        public Alert()
        {
            InitializeComponent();
        }
    }
}

自动生成的 .g.cs 文件,错误所在的位置:

#pragma checksum "..\..\..\Views\Alert.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "9DA5FA58C39040F7B2871131172AF413B91CBB451AD975BC9125BC5CE80A26AE"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using Redacted.Common.Gui.Wpf.Views;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using Wpf.Ui;
using Wpf.Ui.Common;
using Wpf.Ui.Controls;
using Wpf.Ui.Controls.Navigation;
using Wpf.Ui.Converters;
using Wpf.Ui.Markup;
using Wpf.Ui.ValidationRules;


namespace Redacted.Common.Gui.Wpf.Views {
    
    
    /// <summary>
    /// Alert
    /// </summary>
    public partial class Alert : Wpf.Ui.Controls.UiWindow, System.Windows.Markup.IComponentConnector {
        
        private bool _contentLoaded;
        
        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("/Redacted.Common.Gui.Wpf;component/views/alert.xaml", System.UriKind.Relative);
            
            #line 1 "..\..\..\Views\Alert.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);
            
            #line default
            #line hidden
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
            this._contentLoaded = true;
        }
    }
}

错误(一行“public partial class Alert : Wpf.Ui.Controls.UiWindow, System.Windows.Markup.IComponentConnector”):

The type or namespace name 'Ui' does not exist in the namespace 'Redacted.Common.Gui.Wpf' (are you missing an assembly reference?)  Redacted.Common.Gui.Wpf C:\Redacted\Redacted.Common.Gui.Wpf\obj\Debug\Views\Alert.g.cs  48

如何在不更改自己项目的命名空间的情况下解决问题?

C# WPF XAML 命名空间

评论

0赞 mm8 8/23/2023
如何从头开始重现您的问题?您应该能够将指令添加到 中生成的 XAML 文件中。using Wpf.Ui.Controls;Redacted.Common.Gui.Wpf

答: 暂无答案