Windows Phone 7 上的列表框上的捏合缩放

发布于 2024-10-08 19:05:05 字数 1132 浏览 5 评论 0原文

我正在尝试向数据绑定列表框添加捏合缩放功能。做到这一点最有效的方法是什么?我已将列表框放置在网格控件内并使其可滚动。

这是我当前的代码。

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,0,10,10" Background="Black" >
        <ListBox Name="lstText" FontSize="24"  Foreground="White" SelectionMode="Single" Margin="10,0,10,10"  ScrollViewer.VerticalScrollBarVisibility="Visible"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel >
                        <TextBlock Text="{Binding Text}" TextWrapping="Wrap"></TextBlock>                           
                    </StackPanel>                        
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener 
            Tap="GestureListener_Tap" 
            PinchCompleted="GestureListener_PinchCompleted"
            Flick="GestureListener_Flick">

        </toolkit:GestureListener>
    </toolkit:GestureService.GestureListener>

I am trying to add pinch to zoom feature to a data bound ListBox. What is the most efficient way to do this? I have placed the ListBox inside a Grid control and made it scrollable.

This is my current code.

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,0,10,10" Background="Black" >
        <ListBox Name="lstText" FontSize="24"  Foreground="White" SelectionMode="Single" Margin="10,0,10,10"  ScrollViewer.VerticalScrollBarVisibility="Visible"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel >
                        <TextBlock Text="{Binding Text}" TextWrapping="Wrap"></TextBlock>                           
                    </StackPanel>                        
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener 
            Tap="GestureListener_Tap" 
            PinchCompleted="GestureListener_PinchCompleted"
            Flick="GestureListener_Flick">

        </toolkit:GestureListener>
    </toolkit:GestureService.GestureListener>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半﹌身腐败 2024-10-15 19:05:05

列表框不设计为缩放(通过捏合或任何其他方法)。

如果您想实现这一点,您必须以不同的缩放级别重新绘制内容。
不过,您需要克服许多问题:

  • 如何通知用户他们可以通过这种方式更改文本大小?
  • 如何避免影响滚动和选择列表框中项目的标准行为。
  • 对于换行和当前显示的文本,滚动应该如何表现?
  • 列表不应用于在手机上显示大量文本。如果您需要显示大量文本,请在列表中添加一个简短的“标题”,然后在另一个页面中显示详细信息。这样,列表中的文本始终可以以足够大的方式显示,无需更改并且始终可读。
  • 这是您正在努力克服的真正问题还是您认为拥有的东西会很好?手机不仅仅用于您的应用程序,所以如果用户仍然需要在操作系统和其他应用程序中使用具有固定文本大小的列表,那么为什么您需要这个呢?
  • 当您更改文本大小时,框架会重新绘制列表中的所有内容,因此您可能会遇到潜在的性能问题。您可以考虑使用延迟加载,只需在缩放时重新绘制屏幕上显示的内容,但这将影响您如何确定尺寸变化时显示的顶部(和底部)。

总结:这几乎肯定是不必要的,而且会非常复杂并且很难做好。如果你真的想尝试一下,然后发布任何问题的代码。

The Listbox isn't designed to be zoomed (via pinching or any other method).

If you want to implement this you must redraw the content at the different zoom levels.
You'd have a number of issues to overcome though:

  • How do you inform the user that they can alter the text size this way?
  • How do you avoid affecting the standard behaviour for scrolling and selecting items in the listbox.
  • How should scrolling behave with regard to wrapping and the currently shown text?
  • A list shouldn't be used to show large amounts of text on a phone. If you need to show a large amount of text, have a short "title" in the list and then show the detail in another page. This way the text in the list can always be displayed in a way that is large enough that it never need changing and should always be readable.
  • Is this a genuine problem you are trying to overcome or just something you think would be nice to have? The phone won't be used for just your app so why do you need this if the user will still have to use lists with a fixed text size in the OS and other apps.
  • You'd have potential performance issues with performance as the framework redraws everything in the list when you change the size of the text. You could look at use deferred loading to only have to redraw what is shown on the screen while zooming but this will impact how you determine the top (and bottom) of what is shown as the sizes change.

Summary: This is almost certainly unnecessary and will be very complicated and difficult to do well. If you really want to try this have a go and then post code with any problems.

jJeQQOZ5 2024-10-15 19:05:05

Alex Yakhnin 提供了滚动长文本的解决方案。

为 WP7 创建可滚动 TextBlock 。 - Alex Yakhnin 的博客

您可以将 TextBlock 包装在 ScrollViewer 中,这可能足以满足您的需求。如果你的文字足够长,随着文字变大,你会遇到各种各样的困难。 Alex 的解决方案是一个控件,它将 StackPanel 包装在 ScrollViewer 中,并将 TextBlock 添加到 StackPanel 的可管理部分中。

Alex Yakhnin offers a solution for scrolling long text.

Creating Scrollable TextBlock for WP7. - Alex Yakhnin's Blog

You can wrap a TextBlock in a ScrollViewer which may be sufficient for your needs. If your text is long enough you will hit a variety of walls as the text gets larger in size. Alex's solution is a control which wraps a StackPanel in a ScrollViewer and adds TextBlocks to the StackPanel in manageable sections.

乖不如嘢 2024-10-15 19:05:05

我自己用manipulationDelta完成了这一点,但它一点也不顺利

在类属性中

 x:local="clr-namespace:YourApplicationNamespace"

在XAML中:

<Grid x:Name="LayoutRoot"  ManipulationDelta="LayoutRoot_ManipulationDelta">
   <Grid.Resources>
       <local:CustomSettings x:Key="Settings"/>
       <DataTemplate x:Key="verseDataTemplate">
          <TextBlock FontSize="{Binding Path=Font35, Source={StaticResource Settings}}" 
                     Text="{Binding}"/>
       </DataTemplate>
   </Grid.Resources>
   <ListBox ItemTemplate="{StaticResource verseDataTemplate}"/>

在后面的代码中:

private void LayoutRoot_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        try
        {
            var fnt = lboVerses.FontSize;
            if (e.DeltaManipulation.Scale.X == 0 || e.DeltaManipulation.Scale.Y == 0) return;
            if (e.DeltaManipulation.Scale.X > 1 || e.DeltaManipulation.Scale.Y > 1)
            {
                if (fnt < 72)
                   BibliaSettings.font35++;
            }
            else if (e.DeltaManipulation.Scale.X < 1 || e.DeltaManipulation.Scale.Y < 1)
            {
                if (fnt > 5)
                    BibliaSettings.font35--;
            }
        }
        catch (Exception x)
        {
            Debugger.Log(0, "Errors", x.Message + "\n" + x.StackTrace);
        }
    }

您的CustomSettings类

public class CustomSettings : INotifyPropertyChanged
{
    public static List<CustomSettings> Instances;
    public CustomSettings()
    {
        if (Instances == null) Instances = new List<CustomSettings>();
        Instances.Add(this);
    }
    public static int font35
    {
        get 
        {
            return Get("Font35", 35); //Provide mechanism to get settings
        }
        set
        {
            Save(value, "Font35");//Provide mechanism to store settings
            Instances.ForEach(inst => inst.OnPropertyChanged("Font35"));
        }
    }
    public int Font35
    {
        get
        {
            return font35;
        }
        set
        {
            font35=value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

I've done this myself with manipulationDelta but it's not smooth at all

In the class attributes

 x:local="clr-namespace:YourApplicationNamespace"

In the XAML:

<Grid x:Name="LayoutRoot"  ManipulationDelta="LayoutRoot_ManipulationDelta">
   <Grid.Resources>
       <local:CustomSettings x:Key="Settings"/>
       <DataTemplate x:Key="verseDataTemplate">
          <TextBlock FontSize="{Binding Path=Font35, Source={StaticResource Settings}}" 
                     Text="{Binding}"/>
       </DataTemplate>
   </Grid.Resources>
   <ListBox ItemTemplate="{StaticResource verseDataTemplate}"/>

in the code behind:

private void LayoutRoot_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        try
        {
            var fnt = lboVerses.FontSize;
            if (e.DeltaManipulation.Scale.X == 0 || e.DeltaManipulation.Scale.Y == 0) return;
            if (e.DeltaManipulation.Scale.X > 1 || e.DeltaManipulation.Scale.Y > 1)
            {
                if (fnt < 72)
                   BibliaSettings.font35++;
            }
            else if (e.DeltaManipulation.Scale.X < 1 || e.DeltaManipulation.Scale.Y < 1)
            {
                if (fnt > 5)
                    BibliaSettings.font35--;
            }
        }
        catch (Exception x)
        {
            Debugger.Log(0, "Errors", x.Message + "\n" + x.StackTrace);
        }
    }

Your CustomSettings class

public class CustomSettings : INotifyPropertyChanged
{
    public static List<CustomSettings> Instances;
    public CustomSettings()
    {
        if (Instances == null) Instances = new List<CustomSettings>();
        Instances.Add(this);
    }
    public static int font35
    {
        get 
        {
            return Get("Font35", 35); //Provide mechanism to get settings
        }
        set
        {
            Save(value, "Font35");//Provide mechanism to store settings
            Instances.ForEach(inst => inst.OnPropertyChanged("Font35"));
        }
    }
    public int Font35
    {
        get
        {
            return font35;
        }
        set
        {
            font35=value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文