tags:

views:

18

answers:

0

I have a class that inherits from the HyperlinkButton class in Silverlight 3.0:

public class NavigationButton : HyperlinkButton
{
    public void PerformClick()
    {
        this.OnClick();
    }
}

I have a button on a UI that is bound to a command in my ViewModel. The command executes a method that calls a web method in a WCF DataSevice web service. When I call the web method, I specify a method for the callback. That method is executed when the web method returns a value. When the callback method is executed, I invoke a custom event to let the originating method (that was executed by the command bound to the button that was clicked) that the web method has completed. The event handler for this event, when raised, instantiates a new NavigationButton class (refer to example below):

new NavigationButton { NavigateUri = new Uri("http://www.google.com/"), TargetName = "_blank" }.PerformClick();

The problem that I am experiencing is that nothing happens - I expect a new browser window to appear, with the URL for Google, in this case.

Here is the code that creates an instance of my command to be bound to the button:

ExportReportToExcelCommand = new DelegateCommand<object>(ExportReportToExcel, o => true);

Here is the code for the method, ExportReportToExcel, that calls the web method in the WCF data service:

public void ExportReportToExcel(object parameter)
    {
        Entities.BeginExecute<bool>(
                new Uri(
                    "GetExcelReport",
                    UriKind.Relative),
                QueryGetExcelReportFinished,
                _entities);
    }

Here is the code for the callback method, QueryGetExcelReportFinished:

public void QueryGetExcelReportFinished(IAsyncResult result)
    {
        var context = result.AsyncState as DataServiceContext;
        var results = context.EndExecute<bool>(result).FirstOrDefault();

        if (ExcelReportRetrieved != null)
        {
            ExcelReportRetrieved(this, new ReportCompleteEventArgs(null));
        }
    }

And the last code example is for the event handler for the ExcelReportRetrieved event:

public void OnExcelReportRetrieved(object sender, ReportCompleteEventArgs e)
    {
        new NavigationButton { NavigateUri = new Uri("http://www.google.com/"), TargetName = "_blank" }.PerformClick();
    }

I have substituted the actual URL that the app will navigate to with Google's URL. So, the problem occurs in the last method, OnExcelReportRetrieved. I want the application to open a new browser window and navigate, but nothing happens. I confirmed that this method is being invoked.

The reason why I chose to create a custom HyperlinkButton that can be invoked programmatically is because I did research on how to avoid pop-up blocks in IE and Firefox, and found that this was the best approach, as opposed to using the HtmlPage.Window.Navigate method.

I have also found that when I have a button that is bound to a command, that when clicked, if that command executes a method that immediately calls the NavigationButton class, that a new browser window opens successfully, so this leads me to believe that the issue lies in the fact that I am waiting for my async callback method to be called before instantiating the NavigationButton class.

Any advice would be greatly appreciated.

Thanks!

Chris