开发者

Assembly.LoadFile cannot find the file at the design time

开发者 https://www.devze.com 2023-03-19 16:23 出处:网络
My resource files are in another project. In App.xaml.cs in MainwindowProject, I write code as below to load the resource file.

My resource files are in another project. In App.xaml.cs in MainwindowProject, I write code as below to load the resource file.

static ResourceBundle Cre开发者_StackOverflow中文版ateResourceBundle()
        {

             string dllPath = System.IO.Directory.GetCurrentDirectory() + "\\Trade.Resources.dll";
               ResourceBundle resBundle = new ResourceBundle(Assembly.LoadFile(dllPath));
                resBundle.LocaleRoot = "Trade.Resources.locale"; //where my locale files are
                resBundle.AddResourceFile("Resources");

                string culture = "en-US";
                resBundle.Locale = culture;
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
               return resBundle;
}

some usercontrols are using this resBundle. It's working fine at the run time. But, If I Open the parent xaml window at design time, I got this exception and usercontrol cannot be loaded.

The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
   at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
   at System.Reflection.Assembly.LoadFile(String path)

How could i solve this? Thanks.

Edit : Temporary workaround

I just added the interface in Trade.Resources project. And in App.xaml.cs , it's as below.

Type iResType = Type.GetType("Trade.Resources.IRes,Trade.Resources", true);
            ResourceBundle resBundle = new ResourceBundle(Assembly.GetAssembly(iResType));

so far, it's working fine at both design time and run time.


Try setting the "Copy to Output Directory" property of Resource files to Copy Always. By doing this your resource file will be copied to the Bin directory of the executing assembly. Even if this doesn't work, make your Resource an embedded resource and try reading it from manifest


I am not sure how you are fetching file, once you set the Copy to Output Directory" property of Resource files to Copy Always, then just load the file, it should work

0

精彩评论

暂无评论...
验证码 换一张
取 消