ScrollViewer内にこのような長方形を作成しました
<ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Center" Width="728" Canvas.Top="20" d:LayoutOverrides="HorizontalMargin" >
<Rectangle x:Name="musicBG" Fill="#FF0692FD"/>
</ScrollViewer>
アプリの使用中に、MusicBgのサイズが変化し、場合によっては幅が3,000ピクセル程度になります。
musicBG.Width = _songLength*PixelsPerSecond
ただし、scrollViewerをスクロールしている間は、長方形を画面から完全にスクロールできます。
たとえば、このコード行は、長方形を移動したいところまで移動すると、次の値を示します。
if (songScrollViewer.HorizontalOffset > songScrollViewer.ScrollableWidth)
ホリゾンタルオフセットの値は約1200、ScrollableWidthの値は約2900です。
長方形が画面から完全にスクロールされないように、これを適切に行うにはどうすればよいですか?
約1200のHorizontalOffsetは、長方形を目的地までの約半分だけ押し込み、画面からはみ出さないようにすることを期待します。
答え:
多くのフラストレーションの後、BorderやRectangleの代わりにCanvasを使用することで、この問題を解決することができました。この問題が発生した理由を誰かが説明できる場合、およびキャンバスよりもうまく機能するプロセッサ集約型の制御が少ない場合は、ポイントを付与します。
編集:スクリーンショット:
悪いコード:
<ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" Width="720" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Top" Canvas.Top="20" HorizontalAlignment="Left" >
<Border x:Name="musicBG" Background="#FF0692FD" VerticalAlignment="Top" HorizontalAlignment="Left" Height="270" />
</ScrollViewer>
悪いコードで悪いスクロールの画像:
正常に機能するコード:
<ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" Width="720" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Top" Canvas.Top="20" HorizontalAlignment="Left" >
<Canvas x:Name="musicBG" Background ="#FF0692FD" Height="270" >
<Border Background="#FF0692FD" VerticalAlignment="Top" HorizontalAlignment="Left" Height="270" />
</Canvas>
</ScrollViewer>
良いスクロール:悪いスクロールの118秒という小さい数字ではなく、右下に170秒と表示されていることに注意してください。