-1

将 windows.forms 控件添加到 xaml 的任何想法。

我得到了这个代码

    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>

但我有一个例外。我不知道该怎么办。我被困在这里。

下面给出了详细的例外..

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)
4

2 回答 2

1

我可以让它工作的唯一方法是在后面的代码中设置自动完成属性。否则我会看到同样的错误。但它必须在 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>
于 2013-10-28T09:43:47.987 回答
0

正如@WeylandYutani 所说,我从 xaml 代码中删除了自动完成属性。它工作得很好。

首先添加对 WindowsFormsIntegration 和 System.Windows.Forms 的引用

然后添加 using System.Windows.Forms 作为命名空间

XAML

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

自动完成功能

    void Auto_Complete()
    {
        txtAutoProductCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();

        SqlCeCommand com = new SqlCeCommand("SELECT ProductCode FROM Products_Master", con);
        SqlCeDataReader dr;
        con.Open();
        try
        {
            dr = com.ExecuteReader();
            while (dr.Read())
            {
                string aProduct = dr.GetString(0);
                coll.Add(aProduct);
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        txtAutoProductCode.AutoCompleteCustomSource = coll;
        con.Close();
    }

添加 Auto_Complete(); 在初始化组件()之后;

运行。

于 2013-10-28T09:46:01.230 回答