Posts mit dem Label Silverlight werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Silverlight werden angezeigt. Alle Posts anzeigen

Dienstag, 7. Juni 2011

Silverlight 5 beta: Issue with Toolkit controls

With Silverlight 5 beta there is an issue concerning the usage of the Silverlight Toolkit controls. For some of them (in my sample the MenuItem) you get a compile error

“The type or namespace name 'ContextMenu' does not exist in the namespace 'System.Windows.Controls' (are you missing an assembly reference?)”

Oops?! If you use intellisense to select the MenuItem control it exists. So definitively a bug in Silverlight 5. After a quick search a found also a hint in Microsoft Connect: Silverlight Forum: SL5 NumericUpDown control compile error | Microsoft Connect

XAML which produce the error:

<StackPanel>
    <ListBox x:Name="ListBoxTest">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu x:Name="contextMenu1">
                <toolkit:MenuItem Header="Test"></toolkit:MenuItem>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <ListBoxItem Content="Listitem 1"></ListBoxItem>
        <ListBoxItem Content="Listitem 2"></ListBoxItem>
    </ListBox>
</StackPanel>

Workaround by setting the Menuitems with code behind:

New XAML:

<StackPanel>
    <ListBox x:Name="ListBoxTest">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu x:Name="contextMenu1">
                <!--<toolkit:MenuItem Header="Test"></toolkit:MenuItem>-->
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <ListBoxItem Content="Listitem 1"></ListBoxItem>
        <ListBoxItem Content="Listitem 2"></ListBoxItem>
    </ListBox>
</StackPanel>

Code:

public MainPage()
{
    InitializeComponent();
 
    var menuitem = new MenuItem() { Header = "My Menu" };
    //menuitem.Click += new RoutedEventHandler(menuitem_Click);
 
    contextMenu1.Items.Add(menuitem);
}

Freitag, 20. Mai 2011

Silverlight 5: Better MVVM support

With Silverlight 5 you have the posibilty to define Custome Markup Extensions. For example you can define a new extension MethodInvoke which calls the named method in your datacontext in this way. Not longer event triggers or something else necessary.

more information about Silverlight 5 Beta features: Here

image

Sonntag, 15. Mai 2011

Silverlight 5 goes to desktop application platform

With the new version Silverlight 5 (beta available) the sandbox of an Silverlight app will be nearly destroyed. If you run your Silverlight application in elevated trust mode as out-of-browser application you can access now to the whole local file system and using P/Invoke to call a Win32-Api function. This will give us as developers a total new scope for Silverlight applications. We can now concept Silverlight applications that have native access to local resources like scanners, 3rd party applications and the whole file system – I love this, because the necessity of using WPF nearly disappears.

Pete Brown give us a flavour of new Silverlight applications that uses several operating windows:

Silverlight 5: Working with Operating System Windows - Pete Brown's 10rem.net

Dienstag, 8. Februar 2011

Silverlight set initial Focus / Focus popup or ChildWindow

In our current project we had the problem, that we cannot set the Focus to a Control in an initial Login-Dialog (ChildWindow). We try some things like setting the Focus after the Loaded- or PageUpdated-Event – but nothing helps.

After some time of additional research we found out, that the original problem was the missing Focus of the Silverlight Plugin after starting the Silverlight application.

So first step we do this (“HtmlPage.Plugin.Focus” is the magic):

var loginView = new LoginRegistrationView();
loginView.Show();
 
HtmlPage.Plugin.Focus();

After this, we can use the GotFocus-Event of our Login-Form to set the Focus to the UserName-Field of the DataForm at our Login-Form-ChildWindow.

public LoginForm()
{
    InitializeComponent();
    
    this.GotFocus += this.LoginFormGotFocus;
}
private void LoginFormGotFocus(object sender, System.Windows.RoutedEventArgs e)
       {
           if (this.firstCall)
           {
               var userNameTextBox = (TextBox)loginDataForm.Fields["UserName"].Content;
               userNameTextBox.Focus();
               this.firstCall = false;
           }
       }

Mittwoch, 1. Dezember 2010

Hosting Silverlight Application with RIA Services as Cloud Service

We have an existing web project (Silverlight 4, WCF RIA Services) that we wanted to bring to the cloud using Windows Azure.  We added a new project, Windows Azure Cloud Service and added a new Web Role.  After this you can connect your existing web project to this web role. The isolated web project compiles fine, but our new solution brings the following compile error:

    “Error      1             The "CreateRiaClientFilesTask" task failed unexpectedly.System.Web.HttpException (0x80004005): Could not load file or assembly 'msshrtmi' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.Configuration.ConfigurationErrorsException: Could not load file or assembly 'msshrtmi' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Could not load file or assembly 'msshrtmi' or one of its dependencies. An attempt was made to load a program with an incorrect format.”

Strange, what happens and what can we do?

The solution is very easy, but hard to find out: You have to unload your .web project (which hosts the Silverlight application) and edit it the project file.  If you now remove the tags

    <PlatformTarget>AnyCPU</PlatformTarget>
from the project file, your solution can be compiled !

Samstag, 13. November 2010

Using authenticated RIA Services with Windows Phone 7

In my older post RIA Services: Windows Phone 7 and SOAP endpoint | .NET - Red zone : Best practices and latest stuff I have described how a RIA Service can be used as normal WCF SOAP service for example with Windows Phone 7.

But if you want to use RIA Services with Windows Phone 7, there is the problem that Silverlight for Windows Phone 7 does not support authentication headers. But using the instructions here Using Authenticated Ria Services on your WP7 phone - Marcel de Vries, MVP Team System - blog community you can still use the cool authorization mechanism of WCF RIA Services with Windows Phone 7.

The key steps are:

1. Add enableHttpCookieContainer="true" to your ServiceReferences.ClientConfig

After the proxy is generated enable the HttpCookieContainer at your bindings for authentication service and your domain service. You have to remove it for each proxy new configuration to avoid an error message during proxy generation.

   1: <bindings>
   2:             <basicHttpBinding>
   3:                 <binding name="BasicHttpBinding_AuthenticationServiceSoap" maxBufferSize="2147483647"
   4:                          enableHttpCookieContainer="true"
   5:                     maxReceivedMessageSize="2147483647">
   6:                     <security mode="None" />
   7:                 </binding>
   8:                 <binding name="BasicHttpBinding_EmployeeDomainServiceSoap" maxBufferSize="2147483647"
   9:                       enableHttpCookieContainer="true"
  10:                     maxReceivedMessageSize="2147483647">
  11:                     <security mode="None" />
  12:                 </binding>
  13:             </basicHttpBinding>
  14:         </bindings>
2. Store the returned cookie container of your Login-Call for the next service calls
   1: protected virtual void OnLoginCompleted(object sender, LoginCompletedEventArgs e)
   2:        {
   3:            var args = new LoginServiceCompletedEventArgs();
   4:            
   5:            CookieContainer = null;
   6:            
   7:            if (e.Result != null)
   8:            {
   9:                args.User = null;
  10:                args.Error = true;
  11:  
  12:                CookieContainer = _authclient.CookieContainer;
  13:                ViewModelLocator.AuthCookieContainer = _authclient.CookieContainer;

Hint: Don’t be surprised that the cookie container looks empty at this point (count is 0). There is a kind of “magic” cookie still in there.

3. Use the returned cookie container for your proteced domain service calls
   1: var client = new EmployeeDomainServiceSoapClient
   2:                  {
   3:                      CookieContainer = ViewModelLocator.AuthCookieContainer
   4:                  };

Samstag, 6. November 2010

New Silverlight Toolkit for Windows Phone 7 available

The new November 2010 edition of the open source-based Silverlight Toolkit for Windows Mobile developers brings new pre-built controls that they can make your apps easily and refine. The new and exciting elements AutoCompleteBox, ListPicker LongListSelector (this control helps a lot), different page transition effects (page transitions) are now available in the current version. Of course the controls known from the previous version remain included in the scope of the Toolkit (GestureService GestureListener, ContextMenu, DatePicker, TimePicker, ToggleSwitch, WrapPanel).

You can download the toolkit here: Silverlight - Release: Silverlight for Windows Phone Toolkit - Nov 2010

Donnerstag, 28. Oktober 2010

WindowsPhone 7: NavigationService.Back raises Exception

Sometime a NavigationService.Back call could raise an exception, if the user tombstoned your Windows phone 7 app and you try to call a Navigations.Back after this.

Possible szenario: You ask the user “Do you really want to delete this item?”. After returning a OK from your MessageBox you delete the item and in all cases you want to return from your detail page to your list page with NavigationService.Back.  To avoid a exception you have to put your NavigationService.Back call in a try..catch statement, because the user could press the windows button at your Messagebox and after this the CLR will raises a Cancel for your messagebox and executes the rest of your code with the NavigationService.Back call. Guess from me: Because this situation is not handled by the Silverlight for Windows Phone framework a exception will occur.

Trauriges SmileyTrauriges SmileyTrauriges Smiley

Windows Phone 7: Tombstoning and Messagebox

For a good usabilty for your Windows Phone 7 app you should consider to save your Messagebox text as transient data for example with help of the PhoneApplicationService.Current.State Dictionary. Why is this necessary? For example if the user gets a call or wants to switch to a other ap by pressing the windows button and have not read the Messagbox the user should shown it again after he return to your app.

We use in our project the following class which do all the work for us.

The Show-methods wrapps the orginal MessageBox-Show methods (one override is missing, because a show method with return value for questions make no sense. After tombstoning you loose your functional context, so this Messagebox type should be handled). The ShowFromTransientValues-Method is the key for the suggested behaviour and can be called after you bring your application in a proper state – for example in the first PageLayoutUpdated-Event after reactivation.

HINT: The MessageBox.Show method will return in case of tombstoning always a cancel value. That is a fact that you should keep in mind for Windows Phone 7 programming.

   1: public static class TransientMessageBox
   2:     {
   3:         const string MessageTextKey = "TransientMessageBoxText";
   4:         const string MessageCaptionKey = "TransientMessageBoxCaption";
   5:  
   6:         public static void ShowVirtual(string messageBoxText)
   7:         {
   8:             TransientStorageHelper.AddOrUpdate(MessageTextKey, messageBoxText);
   9:         }
  10:  
  11:         public static void ShowVirtual(string messageBoxText, string caption)
  12:         {
  13:             TransientStorageHelper.AddOrUpdate(MessageTextKey, messageBoxText);
  14:             TransientStorageHelper.AddOrUpdate(MessageCaptionKey, caption);
  15:         }
  16:  
  17:         public static void Show(string messageBoxText)
  18:         {
  19:             ShowVirtual(messageBoxText);
  20:  
  21:             var result = MessageBox.Show(messageBoxText);
  22:  
  23:             Debug.WriteLine("Transient MessageBox result:" + result);
  24:  
  25:             if (result == MessageBoxResult.OK)
  26:             {
  27:                 RemoveTextKey();
  28:             }
  29:         }
  30:  
  31:         public static void Show(string messageBoxText, string caption)
  32:         {
  33:             ShowVirtual(messageBoxText, caption);
  34:  
  35:             var result = MessageBox.Show(messageBoxText, caption, MessageBoxButton.OK);
  36:  
  37:             Debug.WriteLine("Transient MessageBox result:" + result);
  38:  
  39:             if (result == MessageBoxResult.OK)
  40:             {
  41:                 RemoveAllKeys();
  42:             }
  43:         }
  44:  
  45:         private static void RemoveAllKeys()
  46:         {
  47:             TransientStorageHelper.Remove(MessageTextKey);
  48:             TransientStorageHelper.Remove(MessageCaptionKey);
  49:         }
  50:  
  51:         private static void RemoveTextKey()
  52:         {
  53:             TransientStorageHelper.Remove(MessageTextKey);
  54:         }
  55:  
  56:         public static void ShowFromTransientValues()
  57:         {
  58:             if (!PhoneApplicationService.Current.State.ContainsKey(MessageTextKey)) return;
  59:  
  60:             if (PhoneApplicationService.Current.State.ContainsKey(MessageCaptionKey))
  61:             {
  62:                 if (MessageBox.Show(TransientStorageHelper.GetValue<string>(MessageTextKey),
  63:                                 TransientStorageHelper.GetValue<string>(MessageCaptionKey),
  64:                                 MessageBoxButton.OK) == MessageBoxResult.OK)
  65:                 {
  66:                     RemoveAllKeys();
  67:                 }
  68:             }
  69:             else
  70:             {
  71:                 if (MessageBox.Show(TransientStorageHelper.GetValue<string>(MessageTextKey)) == MessageBoxResult.OK)
  72:                 {
  73:                     RemoveTextKey();
  74:                 }
  75:             }
  76:         }
  77:     }