Monday, May 31, 2010

STEPS FOR XBAP SAMPLE DEPLOYMENT

The following are the steps for deploying the XBAP samples.

1. VIRTUAL DIRECTORY CREATION IN IIS

Create a Virtual Directory in local machine with local path. Please use the following steps to do this.

I. Open Internet Information Services (IIS) Manager. (Start -> Run -> inetmgr.exe).
II. Navigate to (Machine name -> Sites -> Default Web Sites).
III. Right click on “Default Web Sites” and select “Add Application option”.



IV. It will open “Add Application” dialog box. From this dialog box, please enter the “Alias” and “Physical Path”. Then Click OK button.




V. It will create a “Virtual directory” with the name of “Sample” to the physical path of “E:\Work\SourceCode\Version\Project\Control”.


2. WORKING SAMPLE APPLICATION WITH VS IDE


We should always work with Admin mode for deploying the XBAP application since we don’t have rights to access IIS in non-admin mode. Open VS IDE in Admin mode.




3. DEPLOYING SAMPLE APPLICATION


I. Open the solution file.

II. Do necessary changes in the application and build it.

III. Once the build get succeeded then open the properties of “Sample” application.

IV. From the properties window, go to Signing tab and make sure that “Sign the ClickOnce manifests” check box is checked. If not, select the certificate key from the Certificate storage.



V. Then navigate to “Security” tab and make sure that “This is a full trust application” radio button is in checked condition. Normally XBAP applications are run within a security sandbox to prevent untrusted applications from controlling local system resources. (E.g. deleting local files). In order to access local file system, port access, bitmap effects, etc., you need to run your application in “Full trust” mode.




VI. After that, select “Publish” tab and enter the publish location (Both local path and internet path where we would like to Run the application) and publish version information.




VII. Before that, make sure that the files needed to include with the application along with the published folder. This can be achieved with help of “Application Files” dialog by clicking the “Application Files” button.




VIII. We need to mention the “Prerequisites” to run our application. This will be provided by using “Prerequisites” dialog.




IX. Then provide the publishing details which will be shown in the main “index.htm” page.




X. Once you complete all above steps, please use publish wizard and publish the sample application.

4. DEPLOYING AN APPLICATION TO THE REMOTE SERVER
 I. Since we already given the internet URL while publishing the project application, we need to create a sample virtual directory in “Remote server’s IIS”. It will make URL available over the internet.

II. Then place the sample application’s (XBAP application) files and folder from local machine to the remote server.

Monday, May 17, 2010

How to get the DependencyObject under the mouse cursor?

With help of VisualTreeHelper and its HitTest() methods, we can easily find the DependencyObject under the mouse cursor. Use the following code snippet to achieve this.

    List<DependencyObject> hitTestList = null;

    //Raised When Mouse is entered inside Panel
    private void Panel_MouseEnter(object sender, MouseEventArgs e)
    {
        hitTestList = new List<DependencyObject>();       

        Point pt = e.GetPosition(sender as IInputElement);       

        VisualTreeHelper.HitTest(
            sender as Visual, null,
            CollectAllVisuals_Callback,
            new PointHitTestParameters(pt));       

        hitTestList.Reverse();

        DependencyObject elementToFind = null;
        foreach (DependencyObject element in hitTestList)
        {
            if (element.GetType().Name.Equals("ElementToFind"))
            {
                elementToFind = element;
                // ...
                // ...
            }
        }
    }

    HitTestResultBehavior CollectAllVisuals_Callback(HitTestResult result)
    {
        if (result == null || result.VisualHit == null)
            return HitTestResultBehavior.Stop;

        hitTestList.Add(result.VisualHit);
        return HitTestResultBehavior.Continue;
    }

Drop me a comment if you have any queries.

Friday, May 14, 2010

How to load Bitmap image to Image object in WPF

I am doing an application (which I am converting Windows forms 2.0 application into WPF application for rich look) in WPF and I am need of the load the Image from an instance of a System.Drawing.Bitmap. Initially I was struggled to do this. After a few minutes of search in goggle, I found the following solution. With help of Imaging class, we can create a BitmapSource and assign the BitmapSource value to the Image. The following code will help you to do this.

    public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source)
    {
        BitmapSource bitmapSrc = null;
        var hBitmap = source.GetHbitmap();
        try
        {
            bitmapSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        catch (Win32Exception)
        {
            bitmapSrc = null;
        }  
        finally
        {
            NativeMethods.DeleteObject(hBitmap);
        }
        return bitmapSrc;
    }

    internal static class NativeMethods
    {
        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool DeleteObject(IntPtr hObject);
    }

Please check this link for more details.

Tuesday, May 4, 2010

How to close the whole application in WPF Browser based application (XBAP application)


In Windows based WPF application, there is an option to close the whole application using (Application.Current.ShutDown) statement. But the same statement is not working in Web based WPF application (perhaps it will close the application. But not close the IE browser – It showing with the blank inactive tab).  We have an option to close the application along with the IE window by using the following code,

    private void buttonClose_Click(object sender, RoutedEventArgs e)
    {
        WindowInteropHelper wih = new WindowInteropHelper(Application.Current.MainWindow);
        IntPtr ieHwnd = GetAncestor(wih.Handle, 2 /*GA_ROOT*/);
        PostMessage(ieHwnd, 0x10/*WM_CLOSE*/, IntPtr.Zero, IntPtr.Zero);
    }

    [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Auto)]
    private static extern IntPtr GetAncestor(IntPtr hwnd, int flags);   

    [DllImport("user32", CharSet = CharSet.Auto)]
    private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);