添加Windows.Forms的控制到WPF [英] Adding windows.forms control to wpf

查看:95
本文介绍了添加Windows.Forms的控制到WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加Windows.Forms的控制XAML的任何想法。



我得到这个代码



 的xmlns:WF =CLR的命名空间:System.Windows.Forms的;装配= System.Windows.Forms的
< WindowsFormsHost保证金=272,10,396,42WIDTH =240 >
< WF:文本框X:NAME =txtAutoProductCodeAutoCompleteMode =SuggestAppendAutoCompleteSource =CustomSource/>
< / WindowsFormsHost>



但我得到了一个例外。我不知道该怎么做。我在这里卡住了。



以下..



中的详细的异常被赋予

  System.Windows.Markup.XamlParseException了未处理
HResult的= -2146233087
消息= Billing.MainWindow'初始化''引发了异常。'行号'6'和线位9。
来源= PresentationFramework
LINENUMBER = 6
LinePosition = 9
堆栈跟踪:
在System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,布尔skipJournaledProperties ,对象rootObject,XamlObjectWriterSettings设置,开放的基本URI)
在System.Windows.Markup.WpfXamlLoader.LoadBaml(xamlReader xamlReader,布尔skipJournaledProperties,对象rootObject,XamlAccessLevel ACCESSLEVEL,乌里基本URI)
在System.Windows.Markup .XamlReader.LoadBaml(流流,parserContext parserContext,对象的父母,布尔closeStream)
在System.Windows.Application.LoadBamlStreamWithSyncInfo(流流,parserContext PC)
在System.Windows.Application.LoadComponent(URI resourceLocator,布尔bSkipJournaledProperties)
在System.Windows.Application.DoStartup()在System.Windows.Application
< .ctor> b__1(对象未用)
在System.Windows.Threading程序.ExceptionWrapper.InternalRealCall(委托回调,对象指定参数时,的Int32 numArgs)
在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象指定参数时,的Int32 numArgs,代表catchHandler)
。在系统在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回调.Windows.Threading.DispatcherOperation.InvokeImpl()
,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态)
在System.Windows.Threading.DispatcherOperation.Invoke()
在System.Windows.Threading.Dispatcher.ProcessQueue()
在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr的HWND, INT32味精,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)
在MS.Win32.HwndWrapper.WndProc(IntPtr的HWND,味精的Int32,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)
在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)
。在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回调,对象指定参数时,的Int32 numArgs)


解决方案

只有这样我可以得到它的工作是落后在代码中设置自动完成的属性。否则,我看到了同样的错误。但它的InitializeComponent()之后进行。

 公共主窗口()
{
的InitializeComponent( );

txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtAutoProductCode.AutoCompleteCustomSource.Add(ITEM1);
txtAutoProductCode.AutoCompleteCustomSource.Add(ITEM2);
}

<网格和GT;
< WindowsFormsHost保证金=272,10,396,42WIDTH =240>
< WF:文本框X:NAME =txtAutoProductCodeAutoCompleteMode =SuggestAppend/>
< / WindowsFormsHost>
< /网格和GT;


Any idea of adding windows.forms control to xaml.

I got this code

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
                <WindowsFormsHost Margin="272,10,396,42" Width="240">
                    <wf:TextBox x:Name="txtAutoProductCode" AutoCompleteMode="SuggestAppend" AutoCompleteSource="CustomSource" />
                </WindowsFormsHost>

But i got an exception. I am not sure what to do. I am stuck here.

The detailed exception is given below..

System.Windows.Markup.XamlParseException was unhandled
  HResult=-2146233087
  Message='Initialization of 'Billing.MainWindow' threw an exception.' Line number '6' and line position '9'.
  Source=PresentationFramework
  LineNumber=6
  LinePosition=9
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
       at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
       at System.Windows.Application.DoStartup()
       at System.Windows.Application.<.ctor>b__1(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)

解决方案

Only way I could get it to work was to set the autocomplete properties in code behind. Otherwise I see the same error. But it has to be done after InitializeComponent().

public MainWindow()
{
    InitializeComponent();

    txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
    txtAutoProductCode.AutoCompleteCustomSource.Add("item1");
    txtAutoProductCode.AutoCompleteCustomSource.Add("item2");
}

<Grid>
    <WindowsFormsHost Margin="272,10,396,42" Width="240">
        <wf:TextBox x:Name="txtAutoProductCode" AutoCompleteMode="SuggestAppend"/>
    </WindowsFormsHost>
</Grid>

这篇关于添加Windows.Forms的控制到WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆