I using Visual Studio 2010 (.NET 4). I need to create a COM object (in C#) and have no idea how to get started (what type of project to use,etc.)
OK I found the solution and I'll write it here for the common good.
Start VS2010 as administrator.
Open a class library project (exmaple - MyProject).
Add a new interface to the project (see example below).
Add a using System.Runtime.InteropServices; to the file
Add the attributes InterfaceType, Guid to the interface.
You can generate a Guid using Tools->Generate GUID (option 4).
Add a class that implement the interface.
Add the attributes ClassInterface, Guid, ProgId to the interface.
ProgId convention is {namespace}.{class}
Under the Properties folder in the project in the AssemblyInfo file set ComVisible to true.
In the project properties menu, in the build tab mark "Register for COM interop"
Build the project
now you can use your COM object by using it's ProgID.
example:
the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Launcher
{
[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
public interface ILauncher
{
void launch();
}
[ClassInterface(ClassInterfaceType.None), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY"), ProgId("Launcher.Launcher")]
public class Launcher : ILauncher
{
private string path = null;
public void launch()
{
Console.WriteLine("I launch scripts for a living.");
}
}
}
and VBScript using the COM:
set obj = createObject("PSLauncher.PSLauncher")
obj.launch()
and the output will be:
I launch scripts for a living
Creation Steps
Start Visual Studio 2013 as administrator
Install Visual Studio extension Microsoft Visual Studio Installer Projects
Create a class library project (WinFormActivex)
Create your example window form (MainWindow)
Create a new component interface(ILauncher)
Create a new security interface (IObjectSafety)
Create the component control (Launcher) that implement interfaces and launch the window.
Check that all GUIDs are generated by you
Check that the project is marked for COM
Create the setup project (LauncherInstaller) with the primary output of WinFormActivex with the property Register = vsdrpCOM
Install LauncherInstaller
Run your test page in explorer (test.html)
MainWindow
You can create a normal Form, here is pre-generated.
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(42, 23);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(42, 65);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 0;
//
// MainWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "MainWindow";
this.Text = "MainWindow";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
ILauncher
using System.Runtime.InteropServices;
namespace WinFormActivex
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("94D26775-05E0-4B9C-BC73-C06FE915CF89")]
public interface ILauncher
{
void ShowWindow();
}
}
IObjectSafety
[ComImport()]
[Guid("51105418-2E5C-4667-BFD6-50C71C5FD15C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IObjectSafety
{
[PreserveSig()]
int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
}
Launcher
Please generate your GUID here.
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("D100C392-030A-411C-92B6-4DBE9AC7AA5A")]
[ProgId("WinFormActivex.Launcher")]
[ComDefaultInterface(typeof(ILauncher))]
public class Launcher : UserControl, ILauncher, IObjectSafety
{
#region [ ILauncher ]
public void ShowWindow()
{
var f = new MainWindow();
f.StartPosition = FormStartPosition.Manual;
f.Location = Screen.AllScreens[0].Bounds.Location;
f.WindowState = FormWindowState.Normal;
f.WindowState = FormWindowState.Maximized;
f.ShowInTaskbar = false;
f.Show();
}
#endregion
#region [ IObjectSafety ]
public enum ObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
INTERFACE_USES_DISPEX = 0x00000004,
INTERFACE_USES_SECURITY_MANAGER = 0x00000008
};
public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return 0;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
return 0;
}
#endregion
}
test.html
Please check that your CLSID match (Launcher) GUID.
<html>
<head>
<objectname="activexLauncher" style='display:none' id='activexLauncher' classid='CLSID:D100C392-030A-411C-92B6-4DBE9AC7AA5A' codebase='WinFormActivex'></object>
<script language="javascript">
<!-- Load the ActiveX object -->
var x = new ActiveXObject("WinFormActivex.Launcher");
alert(x.GetText());
</script>
</head>
<body>
</body>
</html>
References
Stack Overflow question I always use as reference
Activex tag you should read
Old Microsoft guide
Article on creating the acrivex control with security options
Article about creating the window
You could use a class library project. Declare a type with methods that will be exposed as a COM object.
Make sure that the assembly has been made COM-visible:
And finally register it using regasm.exe:
regasm.exe /codebase mylib.dll
Now the assembly is exposed as a COM object and the type you declared can be consumed by any client that supports COM.
Related
I am working with the newest DevArt Oracle version and created a EDML file that connects to my Oracle 12 database and get the models with the db first approach.
I followed this howto:
https://www.devart.com/entitydeveloper/docs/
So I have my context and my model auto generated:
public partial class KiddataAdminEntities : DbContext
{
#region Constructors
/// <summary>
/// Initialize a new KiddataAdminEntities object.
/// </summary>
public KiddataAdminEntities() :
base(#"name=KiddataAdminEntitiesConnectionString")
{
Configure();
}
/// <summary>
/// Initializes a new KiddataAdminEntities object using the connection string found in the 'KiddataAdminEntities' section of the application configuration file.
/// </summary>
public KiddataAdminEntities(string nameOrConnectionString) :
base(nameOrConnectionString)
{
Configure();
}
private void Configure()
{
this.Configuration.AutoDetectChangesEnabled = true;
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true;
this.Configuration.ValidateOnSaveEnabled = true;
}
#endregion
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Anrede> Anrede { get; set; }
}
Now I try to get it to work in my main in another project (just a simple console application with a start.cs):
KiddataAdminEntities context = new KiddataAdminEntities("User Id=xxxx;Password=xxxx;Server=xx;Direct=True;Sid=xxxx;Persist Security Info=True");
var listOfAnrede = context.Anrede.ToList();
So now I get the error "Keyword user id not supported".
I googled this and I found out that problably EF6 is trying to get a default connection, not an oracle connection with DevArt...
I tried to play with the app.config in different ways but it didnt help.
Now I tried to create my own connection with the DevArt.Data.Oracle provider, like shown here:
https://www.devart.com/dotconnect/oracle/articles/tutorial-connection.html
OracleConnection oc = new OracleConnection();
oc.ConnectionString = constring2;
oc.Open();
var test = oc.ServerVersion;
This works fine, so the connectionstring is okay, but still I can't put these two together. I tried to overload the constructor so I can put in my Connection:
public KiddataAdminEntities(DbConnection con, bool contextOwnsConnection)
: base(con, contextOwnsConnection)
{
}
Then I got the error on
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
That I should not do that...
If you are using XML mapping with Devart Entity Model (*.edml), try this code:
using Devart.Data.Oracle;
using System.Data.EntityClient;
...
OracleConnectionStringBuilder oracleBuilder = new OracleConnectionStringBuilder();
oracleBuilder.UserId = "...";
oracleBuilder.Password = "...";
oracleBuilder.Server = "...";
oracleBuilder.Direct = true;
oracleBuilder.Sid = "...";
oracleBuilder.PersistSecurityInfo = true;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "Devart.Data.Oracle";
entityBuilder.ProviderConnectionString = oracleBuilder.ConnectionString;
entityBuilder.Metadata = #"res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl";
using (Entities context = new Entities(entityBuilder.ToString())) {
var a = context.MyEntity.First();
}
Refer to
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/how-to-build-an-entityconnection-connection-string
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/connection-strings
FYI, you can generate fluent mapping (instead of XML mapping). For this, disable a predefined EntityObject template, enable the DbContext template and set the options:
Fluent Mapping=True in the properties of DbContext template
Metadata Artifact Processing=Do Not Generate Mapping Files in the properties of EntityContextModel
I have developed a custom code generator and deploy it via a VSIX, the problem is I should register assembly via regasm.exe after installing VSIX, but I have seen some projects such as DSLTool with custom code generation that registers automatically, any body knows how can I have automatically registration in my VSIX project?
You should be able to do the following:
0. Remove old (bad practice) COM code
Edit your project build settings to not have "Register for COM interop" checked.
Edit your AssemblyInfo.cs and set ComVisible to false:
[assembly: ComVisible(false)]
Assuming your generator is named MyCodeGenerator, open the definition of MyCodeGenerator and add the attribute:
[ComVisible(true)]
1. Edit your VSIX project to enable generation of a pkgdef file.
Right click your project in Solution Explorer and select Unload Project.
Right click the unloaded project and select Edit MyProject.csproj, where MyProject is the name of your project.
Locate the XML element <GeneratePkgDefFile>.
If the element exists, ensure that its value is set to true.
Otherwise, add the following to the end of the first <PropertyGroup> element in your project file which does not have a Condition attribute (this is almost always the first PropertyGroup in the file).
<GeneratePkgDefFile>true</GeneratePkgDefFile>
Repeat step 3 to set <CopyBuildOutputToOutputDirectory> to true.
Save and close the .csproj file.
Right click the unloaded project in Solution Explorer and select Reload Project.
Open your project's source.extension.vsixmanifest file and locate the <Content> element. Add the following element as a child:
<VsPackage>|%CurrentProject%|</VsPackage>
If your extension does not provide any other content elements, the entire <Content> element would now be this:
<Content>
<VsPackage>|%CurrentProject%|</VsPackage>
</Content>
2. Define the required attribute types
At the end of this answer are sections for ProvideGeneratorAttribute.cs and ProvideAssemblyObjectAttribute.cs. Add these files to your project.
3. Register the code generator class
Open your project's AssemblyInfo.cs.
Assuming your custom code generator class is named MyCodeGenerator, add the following attribute to the assembly info file.
[assembly: ProvideAssemblyObject(typeof(MyCodeGenerator))]
4. Associate your code generator with the language service
Open your project's AssemblyInfo.cs.
Assuming your custom code generator class is named MyCodeGenerator, and you want to register the code generator with the C# language service, add the following attribute to the assembly info file.
[assembly: ProvideGenerator(
typeof(MyCodeGenerator),
VSConstants.UICONTEXT.CSharpProject_string,
Description = "Description of the generator",
GeneratesDesignTimeSource = true)]
Appendix A: ProvideGeneratorAttribute.cs
Disclaimer: This code is completely untested.
using System;
using Microsoft.VisualStudio.Shell;
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class ProvideGeneratorAttribute : RegistrationAttribute
{
private readonly Type _generatorType;
private readonly Guid _languageServiceGuid;
private string _name;
private string _description;
private bool _generatesDesignTimeSource;
public ProvideGeneratorAttribute(Type generatorType, string languageServiceGuid)
{
if (generatorType == null)
throw new ArgumentNullException("generatorType");
if (languageServiceGuid == null)
throw new ArgumentNullException("languageServiceGuid");
if (string.IsNullOrEmpty(languageServiceGuid))
throw new ArgumentException("languageServiceGuid cannot be empty");
_generatorType = generatorType;
_languageServiceGuid = new Guid(languageServiceGuid);
_name = _generatorType.Name;
}
public Type GeneratorType
{
get
{
return _generatorType;
}
}
public Guid LanguageServiceGuid
{
get
{
return _languageServiceGuid;
}
}
public string Name
{
get
{
return _name;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (string.IsNullOrEmpty(value))
throw new ArgumentException("value cannot be empty");
_name = value;
}
}
public string Description
{
get
{
return _description;
}
set
{
_description = value;
}
}
public bool GeneratesDesignTimeSource
{
get
{
return _generatesDesignTimeSource;
}
set
{
_generatesDesignTimeSource = value;
}
}
private string RegistrationKey
{
get
{
return string.Format(#"Generators\{0}\{1}", LanguageServiceGuid.ToString("B"), Name);
}
}
public override void Register(RegistrationContext context)
{
using (Key key = context.CreateKey(RegistrationKey))
{
if (!string.IsNullOrEmpty(Description))
key.SetValue(string.Empty, Description);
key.SetValue("CLSID", GeneratorType.GUID.ToString("B"));
key.SetValue("GeneratesDesignTimeSource", GeneratesDesignTimeSource ? 1 : 0);
}
}
public override void Unregister(RegistrationContext context)
{
context.RemoveKey(RegistrationKey);
}
}
Appendix B: ProvideAssemblyObjectAttribute.cs
Disclaimer: This code is completely untested.
using System;
using Microsoft.VisualStudio.Shell;
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class ProvideAssemblyObjectAttribute : RegistrationAttribute
{
private readonly Type _objectType;
private RegistrationMethod _registrationMethod;
public ProvideAssemblyObjectAttribute(Type objectType)
{
if (objectType == null)
throw new ArgumentNullException("objectType");
_objectType = objectType;
}
public Type ObjectType
{
get
{
return _objectType;
}
}
public RegistrationMethod RegistrationMethod
{
get
{
return _registrationMethod;
}
set
{
_registrationMethod = value;
}
}
private string ClsidRegKey
{
get
{
return string.Format(#"CLSID\{0}", ObjectType.GUID.ToString("B"));
}
}
public override void Register(RegistrationContext context)
{
using (Key key = context.CreateKey(ClsidRegKey))
{
key.SetValue(string.Empty, ObjectType.FullName);
key.SetValue("InprocServer32", context.InprocServerPath);
key.SetValue("Class", ObjectType.FullName);
if (context.RegistrationMethod != RegistrationMethod.Default)
_registrationMethod = context.RegistrationMethod;
switch (RegistrationMethod)
{
case Microsoft.VisualStudio.Shell.RegistrationMethod.Default:
case Microsoft.VisualStudio.Shell.RegistrationMethod.Assembly:
key.SetValue("Assembly", ObjectType.Assembly.FullName);
break;
case Microsoft.VisualStudio.Shell.RegistrationMethod.CodeBase:
key.SetValue("CodeBase", context.CodeBase);
break;
default:
throw new InvalidOperationException();
}
key.SetValue("ThreadingModel", "Both");
}
}
public override void Unregister(RegistrationContext context)
{
context.RemoveKey(ClsidRegKey);
}
}
I need help. I create Windows Service with Autofac container.
And I use MEF Integration service for create several alternative components for my service.
For example:
Module 1
[Export(typeof(IClass1))]
public class Class1 : IClass1
{
public void Show()
{
Console.WriteLine("Hallo from Class1");
}
}
Module 2
[Export(typeof(IClass2))]
public class Class2 : IClass2
{
public void Show()
{
Console.WriteLine("Hallo from Class2");
}
}
Basic class for modules integration - example
class Program
{
private static IContainer Container { get; set; }
static void Main(string[] args)
{
// Create your builder.
var builder = new ContainerBuilder();
/** find all modules in selected folder */
var catalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\modules", "*Module.dll");
/** register finded modules */
builder.RegisterComposablePartCatalog(catalog);
builder.RegisterType<MyClass>().As<IMyClass>().SingleInstance();
Container = builder.Build();
var cls = Container.Resolve<IMyClass>();
cls.Show();
Console.WriteLine("Class ready. Press Enter");
Console.ReadKey(true);
}
}
class MyClass: IMyClass
{
private readonly IClass1 _class1;
private readonly IClass1 _class3;
private readonly IClass2 _class2;
private readonly IClass2 _class4;
public MyClass(IClass1 class1, IClass2 class2)
{
_class1 = class1;
_class2 = class2;
_class3 = class1;
_class4 = class2;
}
public void Show()
{
_class1.Show();
Console.WriteLine("Class1 ready. Press Enter");
Console.ReadKey(true);
_class2.Show();
Console.WriteLine("Class1 ready. Press Enter");
Console.ReadKey(true);
}
}
internal interface IMyClass
{
void Show();
}
In this example all work fine.This principle I use in my service. For test start and debug my service I use Service.Helper from Nuget packages repository.
Everithyng work fine too.
But. If i create install package in Advance installer and install my service in system (Windows 8.1 x64) service do not start.
Logging exception from service write System.ArgumentNullException in system Event log. Exception most likely in this line
builder.RegisterComposablePartCatalog(catalog);
Service do not load any modules from start folder. Access denied from service to his subfolder. Help please. Thanks.
Try Assembly.GetEntryAssembly().Location insted of Assembly.GetExecutingAssembly().Location
I am learning ASP.NE4 MVC3. I have created a NinjectDependencyResolver class, but I want to know how I would go about implementing the ServiceLocator class. Currently I get this error "The type SportsStore.WebUI.Infrastructure.NinjectDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.
Parameter name: commonServiceLocator".
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
RegisterDependencyResolver();
//ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
NinjectDepencyResolver cs
public class NinjectDependencyResolver
{
private readonly IKernel _kernel;
public NinjectDependencyResolver(IKernel kernel)
{
_kernel = kernel;
}
public object GetService(Type serviceType)
{
return _kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _kernel.GetAll(serviceType);
}
catch (Exception)
{
return new List<object>();
}
}
Your NinjectDependencyResolver must inherit from IDependencyResolver so your code should look like this:
public class NinjectDependencyResolver : IDependencyResolver
I would not do it like that. For one thing, Mark Seemann's book "Dependency Injection in .NET" clearly shows that Service Locator is actually an anti-pattern.
At any rate try not to bloat your global.asax file
If you instead used Nuget and got the latest version of NinjectMVC3 , you should end up with a clean Application_Start method
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
However, if you want to you can add in this line into the end of that method as I believe this is what Adam and Steve do in the Sportstore application in the Apress MVC3 book.
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
Since that book was released, Ninject released newer versions that make it much easier, in fact I would guarantee that the Apress MVC4 book that ends up coming out will show the simpler way. The simple way is use nuget and get NinjectMVC3 , then it will have an App_Start folder which will run the files in them at start of the application.
Here is an example of it with some bindings
using Products.Data.Abstract;
using Products.Data.Concrete;
using Products.Data.Infrastructure;
[assembly: WebActivator.PreApplicationStartMethod(typeof(ProductsWeb.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(ProductsWeb.App_Start.NinjectMVC3), "Stop")]
namespace ProductsWeb.App_Start
{
using System.Reflection;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Mvc;
public static class NinjectMVC3
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IProductsRepository>().To<FakeProductsRepository>();
kernel.Bind<MovieRepository>().To<MovieRepository>();
}
}
}
Why not just use the official MVC Integration extension for Ninject, and the Common Service Locator implementation that comes in the official main distribution of Ninject (the dll is included in the build downloads)?
I'm trying to understand how Caliburn.Micro works with Windows Phone (and MVVM in general) so I created a basic Windows Phone Application, installed Caliburn.Micro NuGet package (v1.2.0 - the latest for now) and followed the few instructions here and there. So, I ended up with:
WMAppManifest.xml
<DefaultTask Name ="_default" NavigationPage="Views/HomeView.xaml"/>
Framework/AppBootstrapper.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using Caliburn.Micro;
using MyCaliburn.PhoneUI.ViewModels;
namespace MyCaliburn.PhoneUI.Framework
{
public class AppBootstrapper : PhoneBootstrapper
{
PhoneContainer container;
protected override void Configure()
{
container = new PhoneContainer(RootFrame);
container.RegisterPhoneServices();
container.Singleton<HomeViewModel>();
}
protected override void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
Debugger.Break();
e.Handled = true;
}
else
{
MessageBox.Show("An unexpected error occured, sorry about the troubles.", "Oops...", MessageBoxButton.OK);
e.Handled = true;
}
base.OnUnhandledException(sender, e);
}
protected override object GetInstance(Type service, string key)
{
return container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
}
}
ViewModels/HomeViewModel.cs
using Caliburn.Micro;
namespace MyCaliburn.PhoneUI.ViewModels
{
public class HomeViewModel : Screen
{
public HomeViewModel()
{
//DisplayName = "Home";
}
}
}
View/HomeView.xaml.cs (the XAML page is the default Window Phone Portrait Page)
using Microsoft.Phone.Controls;
namespace MyCaliburn.PhoneUI.Views
{
public partial class HomeView : PhoneApplicationPage
{
public HomeView()
{
InitializeComponent();
}
}
}
App.xaml
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyCaliburn.PhoneUI.App"
xmlns:Framework="clr-namespace:MyCaliburn.PhoneUI.Framework">
<!--Application Resources-->
<Application.Resources>
<Framework:AppBootstrapper x:Key="bootstrapper" />
</Application.Resources>
</Application>
App.xaml.cs
using System.Windows;
namespace MyCaliburn.PhoneUI
{
public partial class App : Application
{
/// <summary>
/// Constructor for the Application object.
/// </summary>
public App()
{
// Standard Silverlight initialization
InitializeComponent();
}
}
}
Now, when I hit F5, the application runs and exits without showing any page or exception and doesn't hit any breakpoints that I sit.
Can anyone tells me what's missing in my code which prevents the application from running?
Thanks in advance.
Many times when I end up with an app that does not start - it turns out that due to some refactoring the App class is not the startup object any more. Right-click on the project in solution explorer, go to properties/Application and make sure Startup object is set correctly.