我可以在资源文件中引用非WPF类吗? [英] Can I reference non-WPF classes in a resource file?

查看:90
本文介绍了我可以在资源文件中引用非WPF类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我可以在另一个项目(mvvm中的 view 文件夹)中引用名称空间,但兄弟视图模型和模型文件夹中的名称空间和类是不可访问的,即使我手动输入它,它声称它不存在。



我认为这不是标准行为,因为这些视图模型可以在同一个文件中访问,直到我重构代码库并将每个模块分成这是自己的项目。



我的尝试:



我' ve在项目参考中添加了所有3个当前项目。我该如何访问这些文件夹?问题是什么?非常感谢。



项目文件可以在这里找到 Dirkstar99。 7z - Google云端硬盘 [ ^ ]

解决方案

你能展示一些示例代码吗?



你很可能错过了像这样的汇编参考:

 

xmlns:conv =clr-namespace:MRULib.Converters; assembly = MRULib



如下所示:

主要的MRULib / MainWindow.xaml·Dirkster99 / MRULib·GitHub [ ^ ]



但由于您没有发布示例代码,我无法分辨: - (

我很高兴你可以下载你的代码 - 它包含你需要修复的多个问题才能编译:



替换跟随StringRes.xaml中的XAML

 xmlns:topicsmd =clr-namespace:SchoolPrism.Topics.Model
xmlns:topicsvm =clr-namespace: SchoolPrism.Topics.ViewModel>





with



 xmlns:topicsmd =clr-namespace:SchoolPrism.Topics.Model; assembly = Topics
xmlns:topicsvm =clr-namespace:SchoolPrism.Topics.ViewModel; assembly = Topics>





这实际上是我试图在解决方案1中建议的,而没有看到你的代码...



使类

 TopicsViewModel 

 TopicSchema 

公开,因为否则它们将在其程序集之外不可见。



我可以在进行这些更改时编译并启动代码。但后来我得到了一个不相关的PRISM异常:

 


exception {Directory ../../ SchoolPrismInfrastructure/Modules not found。} System.InvalidOperationException

我在下面为你进一步修改。



根据你提出的问题和问题的简单性你似乎无法识别并修复我推荐你在没有PRISM的情况下建立你的项目 - 然后在你有一个工作的应用程序时介绍PRISM - 否则,你将试图一次兼顾一个以上的全新主题,并且一些球会撞到地上(我害怕) ...



我希望这有用,但是......



  public   class  TopicSchema 
{
public string 名称{
get ; set ;
}

public int
{
get ; set ;
}

public Uri VidLink { get ; set ; }

public List< string>说明
{
get ; set ;
}

public string 代码
{
get ; set ;
}
public string VidString
{
< span class =code-keyword> get { return VidString; }
set {
VidLink = new Uri( ../../ Topics / View / Assets / + value ,UriKind.Relative); // 从调试文件夹读取的相对路径
}
}
}





  public   class  TopicsViewModel 
{
public TopicSchema currentTopic
{
获得; set ;
}

public ObservableCollection< TopicSchema> getAllWeeks
{
get { return TopicsModel.GetAllWeeks(); }

set {}
}

public DelegateCommand< string> displayTopic
{
get { return new DelegateCommand< string>(DisplayTopic); }

set {}
}

public void DisplayTopic( string week){

var query = 来自主题 getAllWeeks where topic.Week == int .Parse(week) select 主题;

currentTopic = query.First();

IModule allModules = ServiceLocator.Current.GetInstance< ModuleCatalog>()。Modules.Where((ModuleInfo m)= > m.ModuleName .Equals( TopicsModule))。Cast< IModule>()。First(); // 。ShowSandbox();
}
}





我用它玩了一点,发现了一个简单的解决方案,用于额外的例外。进入解决方案Exporer并:



1)将一个Modules文件夹添加到

 SchoolPrism 

项目和

2)将一个ReadMe.txt文件添加到modules文件夹中

3)右键单击ReadMe.txt文件并选择属性 - 选择Build Action = Content并复制到OutputDirectory =总是



进入

 BootStrapper.cs 

课程并编辑以下几行:

  protected   override  IModuleCatalog CreateModuleCatalog(){

return new DirectoryModuleCatalog(){
ModulePath = @ 。\Modules \ // 执行路径是debug / bin
};
}





这应该确保你总是有一个模块目录,PRISM不会抛出上述异常..当我这样做时,应用程序启动了...不要忘记给解决方案明星......


I noticed I can reference namespaces in another project (the view folder in mvvm) but the namespaces and classes in the sibling view model and model folders are inaccessible, even if I enter it manually, it claims it doesn't exist.

I believe this is not standard behavior because those view models were accessible in the same file until I refactored the codebase and split each module into it's own project.

What I have tried:

I've added all 3 current projects in the projects reference. How can I access those folders? What could the problem be? Many thanks.

The project files may be found here Dirkstar99.7z - Google Drive[^]

解决方案

Can you show some sample code?

More likely than not you are missing an assembly reference like this:

xmlns:conv="clr-namespace:MRULib.Converters;assembly=MRULib"

as shown here:
MRULib/MainWindow.xaml at master · Dirkster99/MRULib · GitHub[^]

But I cannot tell since you post no sample code :-(


I am glad you made your code downloadable - it contains multiple problems that you need to fix to get it compiled:

Replace the following XAML in StringRes.xaml

xmlns:topicsmd="clr-namespace:SchoolPrism.Topics.Model"
xmlns:topicsvm="clr-namespace:SchoolPrism.Topics.ViewModel">



with

xmlns:topicsmd="clr-namespace:SchoolPrism.Topics.Model;assembly=Topics"
xmlns:topicsvm="clr-namespace:SchoolPrism.Topics.ViewModel;assembly=Topics">



This is actually what I tried to suggest in Solution 1 without seeing your code...

Make the classes

TopicsViewModel

and

TopicSchema

public since they will otherwise be invisible outside of their assembly.

I can compile and start your code when I make these changes. But then I get an unrelated PRISM Exception:


exception {"Directory ../../SchoolPrismInfrastructure/Modules was not found."} System.InvalidOperationException

which I fixed further below for you.

Based on the questions you asked and the simplicity of the problems you cannot seem to recognize and fix I recommend you build your project without PRISM first - and then introduce PRISM when you have a working application - otherwise, you'll be trying to juggle more than one completely new topic at a time and some balls will smash into the ground (I am afraid)...

I hope this was helpful, though...

public class TopicSchema
    {
        public string Name {
            get; set;
        }

        public int Week
        {
            get; set;
        }

        public Uri VidLink { get; set; }

        public List<string> Instructions
        {
            get; set;
        }

        public string Code
        {
            get; set;
        }
        public string VidString
        {
            get { return VidString; }
            set {
                VidLink = new Uri("../../Topics/View/Assets/" + value, UriKind.Relative); // relative paths read from debug folder
            }
        }
    }



public class TopicsViewModel
    {
        public TopicSchema currentTopic
        {
            get; set;
        }

        public ObservableCollection<TopicSchema> getAllWeeks
        {
            get { return TopicsModel.GetAllWeeks(); }

            set { }
        }

        public DelegateCommand<string> displayTopic
        {
            get { return new DelegateCommand<string>(DisplayTopic); }

            set { }
        }

        public void DisplayTopic (string week) {

            var query = from topic in getAllWeeks where topic.Week== int.Parse(week) select topic;

            currentTopic = query.First();

            IModule allModules = ServiceLocator.Current.GetInstance<ModuleCatalog>().Modules.Where((ModuleInfo m) => m.ModuleName.Equals("TopicsModule")).Cast<IModule>().First(); //.ShowSandbox();
        }
    }



I played a little more with it and found a simple solution for the additional exception. Go into the Solution Exporer and:

1) add a Modules folder to the

SchoolPrism

project and
2) add a ReadMe.txt file into the modules folder
3) Right-Click on the ReadMe.txt file and select properties - select Build Action=Content and Copy to OutputDirectory = Always

Go into

BootStrapper.cs

class and edit the following lines:

protected override IModuleCatalog CreateModuleCatalog ( ) {

            return new DirectoryModuleCatalog() {
                ModulePath = @".\Modules\" // executing path is debug/bin
            };
        }



this should make sure that you always have a Modules directory and PRISM won't throw the above exception... The application starts up for me when I do this... don't forget to give the solution stars...


这篇关于我可以在资源文件中引用非WPF类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆