C#如何在单实例应用程序中双击打开视频?

C#如何在单实例应用程序中双击打开视频?

问题描述:

我想要一些文件扩展名&想要在我的媒体元素上打开。

像这个扩展名.mp4,.mkv,。avg一些文件。并希望在Maindwindow上打开> mediaelement。



我使用这个单一实例创建我的应用程序方法。



我的mainwindow.xaml:



I want to accosiate some file extension & want to open on my mediaelement.
Like this extension .mp4 , .mkv , .avg some files. And want to open on Maindwindow > mediaelement.

I create my application in single instance using this method .

My mainwindow.xaml:

<mediaelement x:name="mediaelement" loadedbehavior="Play">









我想通过双击打开mediaelement上的视频视频文件或拖放放到我的应用程序.exe文件。



但这怎么可能?



我是什么尝试过:







And I want to open a video on mediaelement by double click video file or drag & drop onto my application .exe file.

But how this possible?

What I have tried:

Quote:

单一实例申请:



WpfSingleInstance.cs:





Single Instance Application:

WpfSingleInstance.cs:


using System;
using System.Threading;
using System.Windows;
using System.IO;
using System.IO.IsolatedStorage;

namespace WpfSingleInstanceByEventWaitHandle
{
	public static class WpfSingleInstance
	{

		internal static void Make(String name, Application app)
		{

			EventWaitHandle eventWaitHandle = null;
			String eventName = Environment.MachineName + "-" + name;

			bool isFirstInstance = false;

			try
			{
				eventWaitHandle = EventWaitHandle.OpenExisting(eventName);
			}
			catch
			{
				// it's first instance
				isFirstInstance = true;
			}

			if (isFirstInstance)
			{
				eventWaitHandle = new EventWaitHandle(
					false,
					EventResetMode.AutoReset,
					eventName);

				ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, waitOrTimerCallback, app, Timeout.Infinite, false);

				// not need more
				eventWaitHandle.Close();


				// !!! delete it if not use
				setFirstArgs();
			}
			else
			{
				// !!! delete it if not use
				setArgs();


				eventWaitHandle.Set();

				// For that exit no interceptions
				Environment.Exit(0);
			}
		}


		private delegate void dispatcherInvoker();

		private static void waitOrTimerCallback(Object state, Boolean timedOut)
		{
			Application app = (Application)state;
			app.Dispatcher.BeginInvoke(
					new dispatcherInvoker(delegate() {
						Application.Current.MainWindow.Activate();

						// !!! delete it if not use
						processArgs();

					}),
					null
				);
		}





		// Args functionality for test purpose and not developed carefuly
		#region Args

		internal static readonly object StartArgKey = "StartArg";

		private static readonly String isolatedStorageFileName = "SomeFileInTheRoot.txt";

		private static void setArgs()
		{
			string[] args = Environment.GetCommandLineArgs();
			if (1 < args.Length)
			{
				IsolatedStorageFile isoStore =
					IsolatedStorageFile.GetStore(
						IsolatedStorageScope.User | IsolatedStorageScope.Assembly,
						null,
						null);

				IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream(isolatedStorageFileName, FileMode.Create, isoStore);
				StreamWriter sw = new StreamWriter(isoStream1);
				string arg = args[1];
				sw.Write(arg);
				sw.Close();
			}
		}


		private static void setFirstArgs()
		{
			string[] args = Environment.GetCommandLineArgs();
			if (1 < args.Length)
			{
				Application.Current.Resources[WpfSingleInstance.StartArgKey] = args[1];
			}
		}


		private static void processArgs()
		{
			IsolatedStorageFile isoStore =
				IsolatedStorageFile.GetStore(
					IsolatedStorageScope.User | IsolatedStorageScope.Assembly,
					null,
					null);


			IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream(isolatedStorageFileName, FileMode.OpenOrCreate, isoStore);
			StreamReader sr = new StreamReader(isoStream1);
			string arg = sr.ReadToEnd();
			sr.Close();

			isoStore.DeleteFile(isolatedStorageFileName);

			Window1.ProcessArg(arg);
		}

		#endregion


	}
}







App.xaml.cs:






App.xaml.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace WpfSingleInstanceByEventWaitHandle
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{



		protected override void OnStartup(StartupEventArgs e)
		{
			WpfSingleInstance.Make("MyWpfApplication",this);

			base.OnStartup(e);
		}

	}
}










请告诉我如何解决?

你必须做两件事:



0)在注册表中设置适当的文件关联



1)编写你的应用程序以便它会在操作系统指示时自动打开文件。



顺便说一句,如果你的应用程序已经开始怎么办?在狭义的世界中,双击不会做任何事情。
You have to do two things:

0) Set the appropriate file association in the registry

1) Code your app so that it automatically opens the file when instructed to by the OS.

BTW, what if your app is already start? In your narrowly defined world, the double-click won't do anything.