【问题标题】:Design View Model as internal class将视图模型设计为内部类
【发布时间】:2015-09-21 12:27:24
【问题描述】:

我正在开发 Windows 运行时组件项目,该项目必须向外界公开很少的控件。我想使用设计视图模型,所以我有类似的东西:

public sealed class FirstPanelDesignViewModel
{ ... }

它在设计时按预期工作,但这个类也暴露于其项目的外部世界,这是不可取的。所以我可以这样修改:

internal class FirstPanelDesignViewModel
{ ... }

但现在设计器(打开的 .xaml 文件)抱怨它无法访问该类并且渲染不再发生:

The name "FirstPanelDesignViewModel" does not exist in the namespace "using:SomeProject.DesignViewModels".

有没有办法在 Windows 运行时组件中使用设计视图模型而不将其暴露在外部?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我猜你可以使用Friend Assemlies

    using System.Runtime.CompilerServices;
    using System;
    
    [assembly: InternalsVisibleTo("AssemblyB")]
    
    // The class is internal by default.
    class FriendClass
    {
        public void Test()
        {
            Console.WriteLine("Sample Class");
        }
    }
    
    // Public class that has an internal method.
    public class ClassWithFriendMethod
    {
        internal void Test()
        {
            Console.WriteLine("Sample Method");
        }
    
    }
    

    【讨论】:

    • 好的,那么我应该使用 InternalsVisibleTo("") 添加哪个程序集,以便设计器看到该类?
    • 你应该添加使用你的类的程序集
    • 但我在包含类的程序集中工作。
    • 哎哟!我认为这是不同的程序集 :) stackoverflow.com/questions/19704149/…
    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2011-12-26
    • 1970-01-01
    相关资源
    最近更新 更多