问题
I really like C# and I am familiar with it, but I also want to use HTML5/JavaScript to manage the UI for my Windows 8 Metro app. So, how can I import and use objects from a library made in C# in the Javascript files?
Example here is the starting JS code for an empty HTML5/JS project...
// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
"use strict";
var app = WinJS.Application;
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize
// your application here.
} else {
// TODO: This application has been reactivated from suspension.
// Restore application state here.
}
WinJS.UI.processAll();
}
};
app.oncheckpoint = function (eventObject) {
// TODO: This application is about to be suspended. Save any state
// that needs to persist across suspensions here. You might use the
// WinJS.Application.sessionState object, which is automatically
// saved and restored across suspension. If you need to complete an
// asynchronous operation before your application is suspended, call
// eventObject.setPromise().
};
app.start();
})();
Can I pull in and use libraries and objects in JS that are written in C#?
I was kind of bummed they appear to segregate C# from HTML5 based projects...
回答1:
You absolutely can do this. This is the beauty of the Windows 8 and the new application model. There are a lot of places to start and look at.
Start here: http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=vs.110).aspx You can drill down further in the above link where it also links to a real basic sample. http://msdn.microsoft.com/en-us/library/windows/apps/hh779077(v=vs.110).aspx
In a nutshell, you'll create a metro class library in C# and then set the output type of your C# from a "Class Library" to a WinMD. You can then reference and use that library in your javascript project.
There is a lot of documentation on building metro apps available at http://msdn.microsoft.com/en-us/library/windows/apps
来源:https://stackoverflow.com/questions/9521047/can-i-use-objects-created-in-c-sharp-within-metro-ui-javascript-code