@ichernob

Почему не применяются изменения к WritableBitmap?

Пробую разобраться с WritableBitmap, но столкнулся с проблемой. При вызове события ImageControl_Tapped ничего не происходит. НО если прямо в методе события создавать битовую карту (подобно тому. как это делается в MainPage(), то изменения видны, но мне нужно, чтобы было видно все сделанные изменения, а не последнее. В чем косяк?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace WritableBitmapApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public WriteableBitmap writeableBmp;

        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

            writeableBmp = BitmapFactory
                .New(512, 350);
            writeableBmp.Clear(Colors.Black);
            ImageControl.Source = writeableBmp;
            writeableBmp.GetBitmapContext();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }

        private void ImageControl_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var pnt = e.GetPosition(ImageControl);

            try
            {
                writeableBmp.DrawLine(0, 0, (int)pnt.X, (int)pnt.Y, Colors.White);
                ImageControl.Source = writeableBmp;
                writeableBmp.GetBitmapContext();
                writeableBmp.Invalidate();
                ImageControl.Source = writeableBmp;
            }
            catch(Exception ex)
            {

            }
        }
    }
}


<Page
    x:Class="WritableBitmapApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WritableBitmapApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Image x:Name="ImageControl"
               Source="Hobbit.png"
               Tapped="ImageControl_Tapped"
               Height="512"
               Width="350"/>
    </Grid>
</Page>
  • Вопрос задан
  • 189 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы