问题
I'd like to keep my Windows Phone 8 Blank App template based view in a different assembly than the assembly containing the application manifest and App.xaml.
I keep receiving a cryptic exception which doesn't help at all in figuring out how to fix it:
- Create a new project from the template Visual C# > Store Apps > Windows Phone Apps > Blank App (Windows Phone).
- Build and deploy, works great. The properties of the MainPage.xaml state the Build Action is Page, which is correct.
- Create a new project based on the Class Library (Windows Phone) template from the same category within the solution containing the original project, call it MyApp.Views.
- Move the MainView.xaml file to the newly created project using Cut and Paste commands.
- Add a reference to MyApp.Views to the original project.
- Build, deploy, see the app start and fail to locate the view only to propagate this exception back to the developer's box:
ComException
: Error HRESULT E_FAIL has been returned from a call to a COM component. This doesn't say absolutely anything at all useful and the top stack frame isWindows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter)
following the first one, which isTheOriginalProject.App.OnLaunched(LaunchActivatedEventArgs e)
.
The solution is not to use NavigationService
with pack URI since it seems to be absent in Windows Phone 8, or at very least the assembly containing it is not referenced by default in the Blank App template. In any event, I'd vastly prefer strongly typed view names over pack URIs.
I imagine someone must've run into this issue already, what's the catch? Is there an API or a tool that will give me an insight on what the latest E_FAIL coming from COM is?
Edit: When using Window.Current.Content = new MainPage()
as the only content of OnLaunched
, the XAML parser exception pops up. It is unable to populate an exception message, but definitely gives a better hint to what's going on. Still no solution, though, the problem only moved to the this.InitializeComponent()
in the MainPage
constructor.
What I see now is Windows.UI.Xaml.Markup.XamlParseException
with WinRT information of Parser internal error: Object writer '%0' and Additional information unable to load. x:Class
attribute is present at the position 128 hinted on by the exception dialog window and this thread seems relevant, but I can't work out how.
Please note that I'll also find values in people confirming not receiving the error I do. If you've tried the steps to reproduce and failed to reproduce the problem, please write a comment so I can pin down what it is that causes the error I'm struggling with.
回答1:
Alternative solution to externalizing views to a separate project while avoiding this problem is to use a Shared project project type, put the views there and reference it from the main project.
This does not produce a separate assembly for the views, instead the files are just grouped within their custom project, but behave as if they were part of the main project during compilation. The error goes away because from the compiler point of this, there's no difference between a file in a shared project and a file directly in the main project. During runtime, the type of the view doesn't need to be resolved from a different assembly, it's right there in the same assembly.
来源:https://stackoverflow.com/questions/28071841/frame-navigate-to-a-page-derived-class-in-a-different-assembly