开发者

Silverlight InvalidOperationException when clicking a link

I have a dynamically generated hyperlink which when clicked should open a lotus notes document. I do it using the code below.

HyperlinkButton hlb = new HyperlinkButton();
hlb.SetBinding(HyperlinkButton.ContentProperty, new Binding("Properties[" + col.DisplayField + "]"));
hlb.SetBinding(HyperlinkButton.NavigateUriProperty, new Binding("Properties[" + col.LinkField + "]"));
hlb.Click += new RoutedEventHandler(hlb_Click);
RootGrid.Children.Add(hlb);

this is the code that fires when the link is clicked.

static void hlb_Click(object sender, RoutedEventArgs e)
{
    HyperlinkButton hlb = (HyperlinkButton)sender;
    var hostingWindow = HtmlPage.Window;
    hostingWindow.Navigate(hlb.NavigateUri);
}

the lotus notes document opens correctly but I get a System.InvalidOperationException, the details of which are given below

Description: Failed to navigate to notes://<path to the document>

Stacktrace:开发者_Go百科
at MS.Internal.NavigationHelper.Navigate(Boolean checkUserInitiatedAction)
at System.Windows.Controls.HyperlinkButton.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Another interesting thing to note is that it is raised on another thread and hence is not caught when the hostingWindow.Navigate method is fired.

Any ideas ?


Using Silverlight 5, I wrapped the call to open the Lotus Notes doc link within a task and was able to open the link without generating an error.

private void TryOpenDocLink()
{
    TaskScheduler ts = TaskScheduler.Default;

    Task<bool> task = OpenDocLink();

    task.ContinueWith(t =>
    {
        if (t.Exception != null)
        {
            this.SetError(t.Exception.Message, enMessageLevel.Error);
        }
    });
}

private Task<bool> OpenDocLink()
{
    TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();

    try
    {
        var hostWindow = HtmlPage.Window;
        hostWindow.Navigate(new Uri(DocLinkPath));
        tcs.SetResult(true);
    }
    catch (Exception)
    {
        tcs.SetResult(false);
    }

    return tcs.Task;
}


Try marking the click event as handled:

static void hlb_Click(object sender, RoutedEventArgs e)
{
    e.Handled = true;
    HyperlinkButton hlb = (HyperlinkButton)sender;
    var hostingWindow = HtmlPage.Window;
    hostingWindow.Navigate(hlb.NavigateUri);
}

I'm not sure that this will fix the issue. The error is coming from the click event code inside the hyperlink button. You can tell because that code uses the NavigationHelper class while the Window.Navigate method does not.

Is there a reason you're not just letting the hyperlink button do the navigation?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜